Python 没有捕获 XMLSyntaxError
我有以下 python + lxml 代码:
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
当我尝试使用损坏的 xml/fb2 文件(已删除标签)对其进行测试时,我希望通过正确的处理获得 XMLSyntaxError 异常。但是,我却因该错误而崩溃:
../.metadata/.plugins/org.python.pydev.debug/.coverage 回溯(最近一次调用最后一次):
文件 “../.eclipse/org.eclipse.platform_3.5.0_155965261/plugins/org.python.pydev.debug_1.5.9.2010063001/pysrc/coverage.py”, 第 1029 行,在 the_coverage.command_line(sys.argv[1:]) 文件 “../.eclipse/org.eclipse.platform_3.5.0_155965261/plugins/org.python.pydev.debug_1.5.9.2010063001/pysrc/coverage.py”, 第 405 行,在命令行中 execfile(sys.argv[0], main._dict_) 文件 "../workspace/PythonPractice/src/lxmlValidation.py", 第 58 行,在 测试()文件“../workspace/PythonPractice/src/lxmlValidation.py”, 第 54 行,测试中 结果= doXMLValidation(源,模式)文件 “../workspace/PythonPractice/src/lxmlValidation.py”, 第 31 行,在 doXMLValidation 中 x_file = etree.parse(xml_file) 文件“lxml.etree.pyx”,第 2692 行,位于 lxml.etree.parse (src/lxml/lxml.etree.c:49594) 文件 “parser.pxi”,第 1500 行,位于 lxml.etree._parseDocument (src/lxml/lxml.etree.c:71364) 文件 “parser.pxi”,第 1529 行,位于 lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:71647) 文件 “parser.pxi”,第 1429 行,位于 lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:70742) 文件 “parser.pxi”,第 975 行,位于 lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:67740) 文件 “parser.pxi”,第 539 行,位于 lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:63824) 文件 “parser.pxi”,第 625 行,位于 lxml.etree._handleParseResult (src/lxml/lxml.etree.c:64745) 文件 “parser.pxi”,第 565 行,位于 lxml.etree._raiseParseError (src/lxml/lxml.etree.c:64088) lxml.etree.XMLSyntaxError:开始和结束标记不匹配:第 7 行和 p,第 7 行,第 46 列
问题可能是什么以及如何解决?
UPD:哈哈,明白了,谢谢大家:
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
我认为解析 lxml 时会更舒服......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在导致错误的语句之前您没有尝试。更仔细地查看堆栈跟踪。
you don't have a try before the statement causing the error. look at the stacktrace more carefully.