0

I want to check for multiple syntax errors in an XML file. For example, missing < or slashes. And not just print the first one like xml.sax or XMLParser do, but show all different errors.

My current function, I try both sax and etree parser:

def check_xml_syntax(xml_file):
    is_valid = True
    try:
        # Parse the XML file
        parser_f = make_parser(  )
        parser_f.setContentHandler(ContentHandler(  ))
        parser_f.parse(xml_file)
        parser = etree.XMLParser(dtd_validation=False)
        etree.parse(xml_file, parser)
        print(f"fctModelFiletoTcl.py: Validation successful: {xml_file} is a valid XML file.")
    except etree.XMLSyntaxError as e:
        is_valid = False
        print(f"fctModelFiletoTcl.py: XML Syntax Error in {xml_file}: {e}")
    except etree.XMLSchemaError as e:
        is_valid = False
        print(f"fctModelFiletoTcl.py: XML Schema Error in {xml_file}: {e}")
    return is_valid
7
  • 1
    Looks like a duplicate of stackoverflow.com/q/11581351/407651 Commented May 15, 2024 at 9:20
  • I'm sorry if this is stupid, but I don't have a XSD file like the answers there require This XML is something internal inside the company Commented May 15, 2024 at 11:01
  • If you don't have an XSD, against what are you validating then? I have no idea what "something internal" means. Commented May 15, 2024 at 11:04
  • Sorry, not a big expert on XML. I just want to know that every open tag is closed, and that the syntax is ok for example my current function catches that I have removed a single < but when I remove 2 , it will only catch the first one Commented May 15, 2024 at 11:24
  • 1
    If you just want to ensure that the syntax is OK, then this is not about validation, it is about well-formedness. See stackoverflow.com/q/134494/407651. Most XML parsers will throw an error and stop at the first instance of bad syntax in an ill-formed document. Commented May 15, 2024 at 11:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.