XML DOM Cloning Nodes

Clone Node

cloneNode() method creates a copy of the specified node.

cloneNode() The method has one parameter (true or falseThis parameter indicates whether the copied node should contain all properties and child nodes of the original node.

The following code snippet copies the first <book> element and appends it to the root node of the document:

Example

oldNode = xmlDoc.getElementsByTagName('book')[0];
newNode = oldNode.cloneNode(true);
xmlDoc.documentElement.appendChild(newNode);

Try It Yourself

Try It Yourself

  1. Example Explanation: Assume books.xml has been loaded to xmlDoc
  2. inGet the node to be copied (oldNode
  3. ) By using cloneNodeMethod to copy the node to "newNode
  4. "