XML DOM Clone Nodes

To create a copy of a specified node, we use the cloneNode() method. The parameter of the cloneNode() method can either be true or false, thus indicating if all the attributes and child nodes of the original node should be included in the cloned node or not.

Books.xml:




  
    ABC
    Author Name
    2020
    29.99
  

  
    Basics
    Author 1
    Author 2
 
 2005
    49.99
  


Example:





Explanation:

In the above example, we are loading the “books.xml” in the xmlDoc to get the old node copied. It means that we will clone the second <book> node into “newNode”, to append the new node to the root node of the XML document.