【发布时间】:2009-07-27 11:46:32
【问题描述】:
我对 minidom 解析器对空元素的处理感到困惑,如下代码部分所示。
import xml.dom.minidom
doc = xml.dom.minidom.parseString('<value></value>')
print doc.firstChild.nodeValue.__repr__()
# Out: None
print doc.firstChild.toxml()
# Out: <value/>
doc = xml.dom.minidom.Document()
v = doc.appendChild(doc.createElement('value'))
v.appendChild(doc.createTextNode(''))
print v.firstChild.nodeValue.__repr__()
# Out: ''
print doc.firstChild.toxml()
# Out: <value></value>
如何获得一致的行为?我想接收 empty string 作为 empty element 的值(这 IS 我首先放在 XML 结构中)。
【问题讨论】: