Ram Rachum
Ram Rachum

Reputation: 88718

Using Python's xml.dom.minidom

I'm trying to use Python's xml.dom.minidom, and I'm getting the following error:

>>> 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'
>>> 

Anyone has any idea why?

Upvotes: 3

Views: 2366

Answers (2)

Isaiah
Isaiah

Reputation: 4309

Replace xdocument.appendChild(xrss) with xrss = xdocument.appendChild(xrss). From the 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.

So you need to assign xrss to the returned element from appendChild.

Upvotes: 0

Luk&#225;š Lalinsk&#253;
Luk&#225;š Lalinsk&#253;

Reputation: 41326

Use xdocument.createElement('name') to create new elements. This is the standard way to do that in DOM.

Upvotes: 3

Related Questions