pyxml minidom: I can remove and append, but not replace

Paul Boddie paul at boddie.net
Wed Apr 23 07:02:02 EDT 2003


reagle at mit.edu (Joseph M . Reagle Jr.) wrote in message news:<3ea5562f$0$22541$b45e6eb0 at senator-bedfellow.mit.edu>...
> I have a snippet that replacing a title element with a new element with a
> modified one (base on an amazon query) -- if that title element already
> exists. However, I can't do an insertBefore or replace for some odd reason:

[...]

>                     if feature:
>                         book.appendChild(r_title)
>                         book.removeChild(feature)
> #                         book.replaceChild(feature,r_title)

[...]

> xml.dom.NotFoundErr: Node does not exist in this context

That's because replaceChild expects the replacement node first in the
argument list, followed by the node to be replaced. What your
replaceChild call is attempting to do is to replace the uninserted
node with the existing node, and obviously, the uninserted node is not
yet present in the document.

> The node is using the same namespace, and I've used the replaceChild in other
> cases (where I'm actually using a node from a different XML instance!)

Strictly, you should use the document node's importNode method on
nodes from other instances before introducing those nodes into the
document.

Paul




More information about the Python-list mailing list