Python ElementTree生成格式不正确的XML文件,带有特殊字符'\ x0b'

问题描述 投票:1回答:1

我使用ElementTree生成具有特殊字符'\ x0b'的xml,然后使用minidom进行解析。它将抛出not well-formed错误。

import xml.etree.ElementTree as ET
from xml.dom import minidom
root = ET.Element('root')
root.text='\x0b'
xml = ET.tostring(root, 'UTF-8')
print(xml)
pretty_tree = minidom.parseString(xml)

生成的XML:<root>\x0b</root>错误:

Traceback (most recent call last):
  File "testXml.py", line 7, in <module>
    pretty_tree = minidom.parseString(xml)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/minidom.py", line 1968, in parseString
    return expatbuilder.parseString(string)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/expatbuilder.py", line 925, in parseString
    return builder.parseString(string)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 6
python xml special-characters non-well-formed
1个回答
0
投票

\x0b是XML限制字符。 this question的答案中对有效和受限制的字符有很好的描述。

© www.soinside.com 2019 - 2024. All rights reserved.