daikini
daikini

Reputation: 1337

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.

Upvotes: 0

Views: 1751

Answers (1)

jcollado
jcollado

Reputation: 40394

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.

Upvotes: 1

Related Questions