1

我正在使用 lxml 包(etree)来获取 xml 模式并使用代码将其解析为 xml 文件。

from lxml import etree
import traceback
schema_file = 'C:/Users/Romi/Desktop/XML Testing/schema.xsd'

def validate(xmlparser, xmlfilename):
try:
    with open(xmlfilename, 'r') as f:
        etree.fromstring(f.read(), xmlparser) 
    return True
except:
    return False

with open(schema_file, 'r') as f:
schema_root = etree.XML(f.read())

schema = etree.XMLSchema(schema_root)
xmlparser = etree.XMLParser(schema=schema)

filenames = ['C:/Users/Romi/Desktop/XML Testing/feed.xml','C:/Users/Romi/Desktop/XML          Testing/feed1.xml' ]
fo = open("C:/Users/Romi/Desktop/XML Testing/result.txt", "r+") 
for filename in filenames:
if validate(xmlparser, filename):
    print "%s validates with the schema." % filename
    #fo.write("%s validates with the schema." % filename)
else:
    print "%s doesn't validate with the schema." % filename
    #fo.write("%s doesn't validate with the schema." % filename)

我在未验证时打印错误,但我想打印指向失败位置的整个回溯,准确给出错误并转到下一个文件进行验证。

任何指针?

4

1 回答 1

3

您可以使用 traceback 库在异常捕获中打印出堆栈跟踪:

http://docs.python.org/2/library/traceback.html#traceback-examples

顺便说一句,限制异常处理是一个好习惯。我将对其进行更改,使其仅捕获 lxml 解析错误 - 例如,如果 open() 失败,您的 validate() 函数将返回 False。

沿着这条线的东西:

try:
  with open(xmlfilename, 'r') as f:
    return etree.fromstring(f.read(), xmlparser)
except etree.XMLSyntaxError:
  print traceback.format_exc()

希望有帮助!

于 2013-07-30T21:55:32.390 回答