## XML DOM: cloneNode() Method
The `cloneNode()` method is a built-in function of the XML DOM `Node` object. It is used to create an exact duplicate of a specified node.
This method is highly useful when you need to replicate existing elements, attributes, or structures within an XML document without manually recreating each node and its children.
---
## Definition and Usage
The `cloneNode()` method duplicates a node and returns the newly created clone.
* The cloned node has no parent node (`parentNode` is `null`) until it is explicitly appended or inserted into the document tree using methods like `appendChild()` or `insertBefore()`.
* Depending on the parameter passed, the clone can either be a **shallow clone** (copying only the node itself and its attributes) or a **deep clone** (copying the node, its attributes, and all of its descendant child nodes).
---
## Syntax
```javascript
var clonedNode = nodeObject.cloneNode(deep);
```
### Parameters
| Parameter | Type | Required | Description |
| :--- | :--- | :--- | :--- |
| `deep` (or `include_all`) | Boolean | Yes (in XML DOM) | Specifies whether to perform a deep clone.
β’ `true`: Clones the specified node **and** all of its descendant child nodes (deep clone).
β’ `false`: Clones **only** the specified node and its attributes, ignoring any child nodes or nested text (shallow clone). |
### Return Value
* **Type:** `Node` object.
* **Description:** The newly created duplicate node.
---
## Code Example
The following example demonstrates how to load an XML file (`books.xml`), clone its first `
` node (including all of its child nodes), and append the cloned node to the end of the document.
### Input XML File (`books.xml`)
```xml
Everyday Italian
Giada De Laurentiis
2005
30.00
Harry Potter
J K. Rowling
2005
29.99
```
### JavaScript Implementation
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books.xml");
// Select the first node
var originalBook = xmlDoc.getElementsByTagName('book');
// Clone the node and all of its children (deep clone)
var clonedBook = originalBook.cloneNode(true);
// Append the cloned node to the root element of the XML document
xmlDoc.documentElement.appendChild(clonedBook);
// Output the text values of all nodes to verify the clone
var titles = xmlDoc.getElementsByTagName("title");
for (var i = 0; i < titles.length; i++) {
document.write(titles.childNodes.nodeValue);
document.write("
");
}
```
### Output
```text
Everyday Italian
Harry Potter
Everyday Italian
```
---
## Important Considerations
1. **Deep vs. Shallow Copying:**
* If you set the parameter to `false`, the node is cloned, but it will contain no text or child elements (since text inside an XML element is technically stored as a child `TextNode`).
* If you set the parameter to `true`, the entire subtree (including elements, attributes, and text) is copied.
2. **Handling IDs:**
* If the original node has an `id` attribute, the cloned node will copy the exact same `id`. To maintain valid XML/HTML document structures, you should update the `id` of the cloned node before appending it back to the document to avoid duplicate identifiers.
3. **Event Listeners (HTML DOM Context):**
* While this is an XML DOM reference, note that in HTML DOM environments, `cloneNode()` does **not** copy event listeners attached via `addEventListener()` or element properties (e.g., `node.onclick = ...`). It only copies inline HTML event attributes (e.g., `onclick="..."`).