YouTip LogoYouTip

Dom Nodes Clone

# XML DOM Cloning Nodes * * * ![Image 2: Example](#) ## Try It - Example The following example uses the XML file [books.xml](#). The function [loadXMLDoc()](#), located in an external JavaScript file, is used to load the XML file. (#) This example uses cloneNode() to clone a node and append it to the root node of the XML document. * * * ## Cloning Nodes The cloneNode() method creates a copy of the specified node. The cloneNode() method has one parameter (true or false). This parameter indicates whether the cloned node should include all attributes and child nodes of the original node. The following code snippet clones the first node and appends it to the root node of the document: ## Example xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); cloneNode=x.cloneNode(true); xmlDoc.documentElement.appendChild(cloneNode); // Output all title node text values y=xmlDoc.getElementsByTagName("title"); for(i=0;i<y.length;i++){document.write(y.childNodes.nodeValue); document.write("
"); } Output: Everyday Italian Harry Potter XQuery Kick Start Learning XML Everyday Italian [Try it Β»](#) Example Explanation: 1. Load "[books.xml](#)" into xmlDoc using [loadXMLDoc()](#) 2. Get the node to be cloned 3. Use the cloneNode method to clone the node into "newNode" 4. Append the new node to the root node of the XML document 5. Output all titles of all books in the document
← Dom HttprequestDom Nodes Add β†’