我正在使用 SAX 来解析大型 xml 文件。但是它将每个XML 代码转换为它的符号版本。
如何防止 SAX 这种行为。
示例with_amp.xml
:
<?xml version="1.0" encoding="utf-8"?>
<root>
<title>One Two</title>
<title>One &mdash; Two</title>
</root>
蟒蛇处理程序:
from xml.sax import handler, parse
class Handler(handler.ContentHandler):
def characters(self, content):
if content.isspace(): return
print(content)
if __name__ == "__main__":
parse(open('with_amp.xml', 'r'), Handler())
我希望输出为:
One Two
One &mdash; Two