2009-10-08 70 views
3

我試圖使用Python的xml.dom.minidom,和我得到了以下錯誤:使用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' 
>>> 

任何人有任何想法,爲什麼?

回答

3

使用xdocument.createElement('name')創建新元素。這是在DOM中執行此操作的標準方法。

0

xdocument.appendChild(xrss)替換爲xrss = xdocument.appendChild(xrss)。從docs

Node.appendChild(newChild) Add a new child node to this node at the end of the list of children, returning newChild. If the node was already in in the tree, it is removed first.

所以,你需要從appendChild分配xrss返回的元素。

相關問題