简体   繁体   English

如何使用python处理libxml2 parserError异常

[英]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. 我尝试编写使用xsl样式表解析xml文件并将其转换为html的warpper。 For broken xml input files I get exception: 对于损坏的xml输入文件,我得到了异常:

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. 我建议使用BeautifulSoup,因为它能够解析格式错误的xml。

In fact, the very first one in the list of features is: 实际上,功能列表中的第一个是:

Beautiful Soup won't choke if you give it bad markup. 如果您给它不好的评分,美丽的汤不会cho。 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. 通常,这足以收集所需的数据并使其消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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