Name

replaceChild: newChild, oldchild

Synopsis

Replaces the child node oldchild with newChild. If newChild is currently linked into the document tree, it is removed before the replace is performed. The method returns a reference to the oldchild node.

Arguments

newChild: Node

The node to be inserted.

oldchild: Node

The node being replaced.

Exceptions

HIERARCHY_REQUEST_ERR

Raised if the insert operation violates at least one document structure rule. For instance, the node doesn’t allow children or doesn’t allow children of the newChild node type. This exception is also raised if the operation creates a circular reference (i.e., it tries to insert a node’s parent as a node’s child).

WRONG_DOCUMENT_ERR

Raised if the newChild node was created in a different document than the new parent node.

NO_MODIFICATION_ALLOWED_ERR

Raised if the new parent node is read-only.

NOT_FOUND_ERR

Raised if the node pointed to by oldchild is not a child of the node performing the replacement.

NOT_SUPPORTED_ERR

Could be raised if the Document node of a DOM implementation in question doesn’t support replacing DocumentType or Element nodes.

Java binding

public Node replaceChild(Node newChild, Node oldChild)
               throws DOMException;

Java example

// Replace an old node with a new one
ndOld.getParentNode( ).replaceChild(ndNew, ndOld);

Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.