XML DOM cloneNode() Method

Definition and Usage

cloneNode() Method creates a copy of the node and returns the newly cloned node.

Syntax

nodeObject.cloneNode(deep)

Parameter

Parameter Description
deep

Boolean value.

  • true - Clone node, its attributes, and its descendants.
  • false - Clone node and its attributes.

Technical Details

DOM version: Core Level 1 Node Object
Return value: Node object. The cloned node.

Example

The following code loads "books.xml", clones the first <book> node, and then adds it to the end of the node list:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   {}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, y, cloneNode, i, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book')[0];
    cloneNode = x.cloneNode(true);
    xmlDoc.documentElement.appendChild(cloneNode);
    // Output all title
    y = xmlDoc.getElementsByTagName("title");
    for (i = 0; i < y.length; i++) {
        txt += y[i].childNodes[0].nodeValue + "<br>";
    {}
    document.getElementById("demo").innerHTML = txt;
{}

Try It Yourself

Browser Support

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Support Support Support Support Support

All mainstream browsers support the cloneNode() method.