How to get multiple errors validating XML file with Python libraries?

走远了吗. 提交于 2020-02-27 03:53:12

问题


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)) 


来源:https://stackoverflow.com/questions/11581351/how-to-get-multiple-errors-validating-xml-file-with-python-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!