1

我想检查与 sample.dtd 关联的 xml 文件 sample.xml 的验证。但我无法得到错误位置。我只能收到错误消息。我怎样才能做到这一点?

import lxml.etree as ET
import codecs

f = codecs.open('sample.dtd')
dtd = ET.DTD(f)
root = ET.parse('newace_JK.xml')
print(dtd.validate(root))
print(dtd.error_log.filter_from_errors())
4

1 回答 1

1

尝试使用单个日志条目而不是打印整个结果,例如

for error in dtd.error_log.filter_from_errors():
    print(error.message)
    print(error.line)
    print(error.column)

http://lxml.de/api/lxml.etree._LogEntry-class.html

于 2013-02-05T12:45:28.783 回答