Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python is not catching XMLSyntaxError

I have the following python + lxml code:

def doXMLValidation (xml_file, schema_file):
    '''Validates xml file against schema'''
    s_doc = etree.parse (schema_file)
    schema = etree.XMLSchema (s_doc)
    x_file = etree.parse(xml_file)

    try:
        schema.assertValid(x_file)
    except etree.XMLSyntaxError as e:
        print (_formatXMLError(e))
        return False
    except etree.DocumentInvalid as e:
        print (_formatXMLError(e))
        return False
    except:
        print ('Something strange...')
        return False
    else:
        return True

When I am trying to test it with a broken xml/fb2 file ( tag removed) I expect to get XMLSyntaxError exception with proper handling. However, instead I get a crash with that error:

../.metadata/.plugins/org.python.pydev.debug/.coverage Traceback (most recent call last):
File "../.eclipse/org.eclipse.platform_3.5.0_155965261/plugins/org.python.pydev.debug_1.5.9.2010063001/pysrc/coverage.py", line 1029, in the_coverage.command_line(sys.argv[1:]) File "../.eclipse/org.eclipse.platform_3.5.0_155965261/plugins/org.python.pydev.debug_1.5.9.2010063001/pysrc/coverage.py", line 405, in command_line execfile(sys.argv[0], main._dict_) File "../workspace/PythonPractice/src/lxmlValidation.py", line 58, in test() File "../workspace/PythonPractice/src/lxmlValidation.py", line 54, in test result = doXMLValidation (source, schema) File "../workspace/PythonPractice/src/lxmlValidation.py", line 31, in doXMLValidation x_file = etree.parse(xml_file) File "lxml.etree.pyx", line 2692, in lxml.etree.parse (src/lxml/lxml.etree.c:49594) File "parser.pxi", line 1500, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:71364) File "parser.pxi", line 1529, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:71647) File "parser.pxi", line 1429, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:70742) File "parser.pxi", line 975, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:67740) File "parser.pxi", line 539, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:63824) File "parser.pxi", line 625, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:64745) File "parser.pxi", line 565, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:64088) lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: a line 7 and p, line 7, column 46

What could the problem be and how to solve it?

UPD: Lol, got it, thanks everyone:

def doXMLValidation (xml_file, schema_file):
    '''Validates xml file against schema'''
    s_doc = etree.parse (schema_file)
    schema = etree.XMLSchema (s_doc)
    try:
        x_file = etree.parse(xml_file)
        schema.assertValid(x_file)
    except etree.XMLSyntaxError as e:
        print (_formatXMLError(e))
        return False
    except etree.DocumentInvalid as e:
        print (_formatXMLError(e))
        return False
    except:
        print ('Something strange...')
        return False
    else:
        return True

Thought lxml would be more agreeable when parsing...

like image 853
Evgeny Avatar asked Nov 28 '25 01:11

Evgeny


1 Answers

you don't have a try before the statement causing the error. look at the stacktrace more carefully.

like image 127
jcomeau_ictx Avatar answered Nov 30 '25 15:11

jcomeau_ictx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!