0

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.

4
  • Is your code running inside a for-loop, what the break is for? Commented Jan 12, 2012 at 11:06
  • Your document cannot be parsed because it's not proper XML. What would you like to have happen when it's not proper XML. You can't make HTML out of it, since it's not proper. What do you want to do when the XML is bad? Commented Jan 12, 2012 at 11:07
  • I would like to inform the user that input file is simply damaged. Commented Jan 12, 2012 at 11:31
  • @thesamet It is not inside loop, sorry my mistake. Commented Jan 12, 2012 at 11:32

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply, I try it willingly but at the moment I would rather that the program does not parse at all if input has bad markup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.