如何使lxml iterparse忽略无效的XML字符?

时间:2013-05-06 10:16:08

标签: python xml xml-parsing lxml

我有一个包含无效字符的XML。 LXML的XMLParser会对这些无效字符引发异常,但是当我使用 recover = True 选项创建XMLParser时,它会忽略不良字符并且工作正常。

我的问题是如何为lxml的iterparse函数设置类似的标志?

繁殖:

破碎的XML(/tmp/z.xml):

<?xml version="1.0" encoding="utf-8"?>
<items>
    <item>
        <B>Bad characters:</B>
    </item>
</items>

注意:“Bad characters:”字符串后面有两个ASCII字符#31(0x1F),我无法复制粘贴到这里。

XMLParser的解析错误:

fd = open('/tmp/z.xml')
parser = etree.XMLParser()
tree   = etree.parse(fd, parser)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lxml.etree.pyx", line 2576, in lxml.etree.parse (src/lxml/lxml.etree.c:22796)
  File "parser.pxi", line 1488, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:60390)
  File "parser.pxi", line 1518, in lxml.etree._parseFilelikeDocument (src/lxml/lxml.etree.c:60687)
  File "parser.pxi", line 1401, in lxml.etree._parseDocFromFilelike (src/lxml/lxml.etree.c:59658)
  File "parser.pxi", line 991, in lxml.etree._BaseParser._parseDocFromFilelike (src/lxml/lxml.etree.c:57303)
  File "parser.pxi", line 538, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:53512)
  File "parser.pxi", line 624, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:54372)
  File "parser.pxi", line 564, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:53770)
lxml.etree.XMLSyntaxError: PCDATA invalid Char value 31, line 4, column 21

要忽略我设置 recover = True 的错误字符,它可以正常工作:

import lxml.etree as etree
fd = open('/tmp/z.xml')
parser = etree.XMLParser(recover=True)
tree   = etree.parse(fd, parser)
etree.tostring(tree)

# OUTPUT:
<items>\n\t<item>\n\t\t<B>Bad characters:</B>\n\t</item>\n</items>'

使用iterparse我再次得到同样的错误,但是如何让它忽略坏字符呢?

fd = open('/tmp/z.xml')
it = etree.iterparse(fd, events=("start", "end"))
for e in it: print e
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "iterparse.pxi", line 498, in lxml.etree.iterparse.__next__ (src/lxml/lxml.etree.c:73245)
  File "parser.pxi", line 564, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:53770)
lxml.etree.XMLSyntaxError: PCDATA invalid Char value 31, line 4, column 21

提前致谢!

0 个答案:

没有答案