XML.cloneNode( ) Method

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
XML.cloneNode( ) Method Flash 5

create an independent copy of a node
theNode.cloneNode(deep)

Arguments

deep

A Boolean indicating whether to recursively include theNode's children in the clone operation. If true, clone the entire hierarchy starting at theNode. If false, clone only theNode itself (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, where theNode can be an XML or XMLnode instance. If theNode is an element node, the clone includes all of theNode's attributes and values. If deep is true, the returned copy includes the entire node hierarchy descending from theNode.

We often use cloneNode( ) to create a new, independent node based on an existing template (which saves us from generating the new node structure manually). Normally, once we've cloned a node, we 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 must set deep to true to preserve an element's text content in a clone operation. Remember that cloneNode( ) does not insert the element it returns into the node's hierarchy we must do that ourselves using appendChild( ) or insertBefore( ).

See Also

XML.appendChild( ), XML.createElement( ), XML.createTextNode( ), XML.insertBefore( )



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net