我正在尝试使用Python的xml.dom.minidom,我得到了以下错误:
>>> from xml.dom import minidom
>>> xdocument = minidom.Document()
>>> xrss = minidom.Element("rss")
>>> xdocument.appendChild(xrss)
<DOM Element: rss at 0xc1d0f8>
>>> xchannel = minidom.Element("channel")
>>> xrss.appendChild(xchannel)
Traceback (most recent call last):
File "C:\Program Files\Wing IDE 3.2\src\debug\tserver\_sandbox.py", line 1, in ?
# Used internally for debug sandbox under external interpreter
File "c:\Python24\Lib\xml\dom\minidom.py", line 123, in appendChild
_clear_id_cache(self)
File "c:\Python24\Lib\xml\dom\minidom.py", line 1468, in _clear_id_cache
node.ownerDocument._id_cache.clear()
AttributeError: 'NoneType' object has no attribute '_id_cache'
>>> 有人知道为什么吗?
发布于 2009-10-08 16:48:33
使用xdocument.createElement('name')创建新元素。这是在DOM中实现这一点的标准方法。
发布于 2009-10-08 16:54:33
用xrss = xdocument.appendChild(xrss)替换xdocument.appendChild(xrss)。从docs
Node.appendChild( newChild )在子节点列表的末尾添加一个新的子节点,返回newChild。如果该节点已在树中,则首先将其删除。
因此,您需要将xrss赋给从appendChild返回的元素。
https://stackoverflow.com/questions/1539023
复制相似问题