How to get multiple errors validating XML file wit

2020-06-27 05:13发布

问题:

I have some XML file i want to validate and i have to do it with Python. I've tryed to validate it with XSD with lxml. But i get only one error which occurs first but i need all errors and mismatches in XML file. Is there any method how i can manage to get list of all errors with lxml? Or are there any other Python solutions?

回答1:

The way to solve this problem was:

try:
    xmlschema.assertValid(xml_to_validate)
except etree.DocumentInvalid, xml_errors:
    pass
print "List of errors:\r\n", xml_errors.error_log

May be there are better ways to solve this problem :)



回答2:

You can read the Python docs on Error exceptions to find out how to do a try/except design pattern, and in the except part you can store the mismatches (a list, or a list of pairs, for example), then recursively try/except again beginning at the point after the first error. Let me know if that isn't descriptive enough.

Cheers!



回答3:

With lxml, you can iterate over error_log and print line number and error message for each error:

def validate_with_lxml(xsd_tree, xml_tree):
    schema = lxml.etree.XMLSchema(xsd_tree)
    try:
        schema.assertValid(xml_tree)
    except lxml.etree.DocumentInvalid:
        print("Validation error(s):")
        for error in schema.error_log:
            print("  Line {}: {}".format(error.line, error.message))


回答4:

The solution provided doesn't work anymore due to several errors. Before applying assertValid on xmlschema, you have to specify it as follows:

try:
   xmlschema_doc = lxml.etree.parse(filename)
   xmlschema = lxml.etree.XMLSchema(xmlschema_doc)
   xmlschema.assertValid(elem_tree)
except lxml.etree.ParseError as e:
   raise lxml.etree.ParserError(str(e))
except lxml.etree.DocumentInvalid as e:
   if ignore_errors:
    raise lxml.etree.ValidationError("The Config tree is invalid with error 
       message:\n\n" + str(e))