Python xml.dom.minidom 生成无效的 XML?
我在 xml.dom.minidom python 包中遇到了奇怪的问题。我生成一个文档,并用从终端获取的数据填充它。有时,此类数据包含终端控制字符。当我使用 minidom.toprettyxml() 将此类字符存储在文本数据节点中时,一切似乎都很好,但是生成的文档不是有效的 XML。
有谁知道为什么 minidom 允许生成无效文档?这与“迷你”部分有关吗?
这是提取的示例代码(还有一些系统信息):
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.dom import minidom
>>> impl = minidom.getDOMImplementation()
>>> doc = impl.createDocument(None, "results", None)
>>> root = doc.firstChild
>>> outString = "test "+chr(1) #here goes control character
>>> root.appendChild(doc.createTextNode(outString))
<DOM Text node "'test \x01'">
>>> doc.toprettyxml(encoding="utf-8")
'<?xml version="1.0" encoding="utf-8"?>\n<results>\n\ttest \x01\n</results>\n'
>>> with open("/tmp/outfile", "w") as f:
... f.write(doc.toprettyxml(encoding="utf-8"))
...
>>> doc2 = minidom.parse("/tmp/outfile")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse
return expatbuilder.parse(file)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 924, in parse
result = builder.parseFile(fp)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile
parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 3, column 6
>>> open("/tmp/outfile","r").readlines()
['<?xml version="1.0" encoding="utf-8"?>\n', '<results>\n', '\ttest \x01\n', '</results>\n']
>>>
I have encountered strange problem with xml.dom.minidom python package. I generate a document, populating it with data taken from terminal. Sometimes such data contain terminal control characters. When I stored such character in text data node with minidom.toprettyxml()
everything seems to be fine, however, the generated document is not a valid XML.
Does anyone know why minidom allows to generate invalid document? Is this connected with "mini" part?
Here is the extracted example code (with some system info too):
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.dom import minidom
>>> impl = minidom.getDOMImplementation()
>>> doc = impl.createDocument(None, "results", None)
>>> root = doc.firstChild
>>> outString = "test "+chr(1) #here goes control character
>>> root.appendChild(doc.createTextNode(outString))
<DOM Text node "'test \x01'">
>>> doc.toprettyxml(encoding="utf-8")
'<?xml version="1.0" encoding="utf-8"?>\n<results>\n\ttest \x01\n</results>\n'
>>> with open("/tmp/outfile", "w") as f:
... f.write(doc.toprettyxml(encoding="utf-8"))
...
>>> doc2 = minidom.parse("/tmp/outfile")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse
return expatbuilder.parse(file)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 924, in parse
result = builder.parseFile(fp)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile
parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 3, column 6
>>> open("/tmp/outfile","r").readlines()
['<?xml version="1.0" encoding="utf-8"?>\n', '<results>\n', '\ttest \x01\n', '</results>\n']
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 _write_data 的代码,它仅转义 & 符号、斜杠和括号:
正如您猜测的那样,minidom 并不是一个完全健壮的实现(例如,它缺乏命名空间的实现)。
Looking at the code for _write_data it only escapes ampersands, slashes and brackets:
As you surmised, minidom isn't intended as a fully robust implementation (its implementation of namespaces is lacking, for instance).