XML DOM cloneNode() Method

Definition and Usage

The cloneNode() method creates an exact copy of the specified node.

This method returns the cloned node.

Syntax:

cloneNode(include_all)
Parameter Description
include_all Must be. If this boolean parameter is set to true, the cloned node will copy all child nodes of the original node.

Example

In all examples, we will use the XML file books.xml, and JavaScript functions loadXMLDoc().

The following code snippet copies the first <book> node and then adds this backup to the end of the node list:

xmlDoc=loadXMLDoc("books.xml");
oldNode=xmlDoc.getElementsByTagName('book')[0];
newNode=oldNode.cloneNode(true);
xmlDoc.documentElement.appendChild(newNode);
//Output all titles
y=xmlDoc.getElementsByTagName("title");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write("<br />");
}

The output of the above code:

Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian