Name
XML.cloneNode( ) Method — create a copy of a node
Availability
Flash 5
Synopsis
theNode.cloneNode(deep)
Arguments
- deep
A Boolean indicating whether to recursively include
theNode’s children in the clone operation. Iftrue, clone the entire hierarchy starting attheNode. Iffalse, clone onlytheNodeitself (and its attributes, if it is an element node).
Returns
A duplicate of the theNode object,
optionally including its subtree.
Description
The cloneNode( ) method creates and returns a
copy of theNode, including all of
theNode’s attributes and values if
theNode is an element node. If
deep is true, the
returned copy includes the entire node hierarchy descending from
theNode.
We often use cloneNode( ) to create a new node
based on an existing template (which saves us from generating the new
node structure manually). Once we’ve cloned a node, we normally
customize it and insert it into an existing XML document using either
appendChild( ) or insertBefore(
). The following example clones the first paragraph of a
document to make a sibling paragraph with the same structure:
// Create a new document
myDoc = new XML('<P>paragraph 1</P>');
// Make a clone of the first paragraph
newP = myDoc.firstChild.cloneNode(true);
// Customize the clone
newP.firstChild.nodeValue = "paragraph 2";
// Add the clone into the document
myDoc.appendChild(newP);
trace(myDoc); // Displays: "<P>paragraph 1</P><P>paragraph 2</P>"Note that the text in an element is stored in a separate child node of that element, so we ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access