简体   繁体   中英

How to handle libxml2 parserError exception with python

I try to write warpper that parses xml files using xsl style sheet and transforms to html. For broken xml input files I get exception:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\USER\Desktop\raportowanie\topsurv raport_beta01\_newest\transmutation     for trimble\testing_transmutation_v.0.6.2.py", line 712, in btnConvertClick
    doc = libxml2.parseFile(filename)
  File "C:\Python27\lib\site-packages\libxml2.py", line 1279, in parseFile
    if ret is None:raise parserError('xmlParseFile() failed')
parserError: xmlParseFile() failed


and tried without success:

try:
    doc = libxml2.parseFile(filename)
except (libxml2.parserError, TypeError):
    print 'error'

In effect I get no exception and parser beahaves as if succeed.
I'd like to ask You for help in this.

I recommend to use BeautifulSoup since it's able to parse malformed xml.

In fact, the very first one in the list of features is:

Beautiful Soup won't choke if you give it bad markup. It yields a parse tree that makes approximately as much sense as your original document. This is usually good enough to collect the data you need and run away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM