cloneNode Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

cloneNode Method

Clones a new node.

[Script]

Script Syntax

var objXMLDOMNode = oXMLDOMNode.cloneNode(deep);

Parameters

deep
Boolean. A flag that indicates whether to recursively clone all nodes that are descendants of this node. If True, creates a clone of the complete tree below this node. If False, clones this node and its attributes only.

Return Value

Object. Returns the newly created clone node.

Example

The following script example clones a node, and then appends it as a child of the top-level node.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
var root;
var currNode;
var MyNewNode;
xmlDoc.async = false;
xmlDoc.load("books.xml")
if (xmlDoc.parseError.errorCode != 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   root = xmlDoc.documentElement;
   currNode = root.childNodes.item(1);
   MyNewNode = currNode.cloneNode(true);
   root.appendChild(MyNewNode);
   alert(xmlDoc.xml);
}
[Visual Basic]

Visual Basic Syntax

Set objXMLDOMNode = oXMLDOMNode.cloneNode(deep)

Parameters

deep
Boolean. A flag that indicates whether to recursively clone all nodes that are descendants of this node. If True, creates a clone of the complete tree below this node. If False, clones this node and its attributes only.

Return Value

An object. Returns the newly created clone node.

Example

The following Microsoft® Visual Basic® example clones a node, and then appends it as a child of the top-level node.

Dim xmlDoc As New Msxml2.DOMDocument50
Dim root As IXMLDOMElement
Dim currNode As IXMLDOMNode
Dim MyNewNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.Load ("books.xml")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set root = xmlDoc.documentElement
   Set currNode = root.childNodes.Item(1)
   Set MyNewNode = currNode.cloneNode(True)
   root.appendChild MyNewNode
   MsgBox xmlDoc.xml
End If
[C/C++]

C/C++ Syntax

HRESULT cloneNode(
    VARIANT_BOOL deep,
    IXMLDOMNode **cloneRoot);

Parameters

deep [in]
A flag that indicates whether to recursively clone all nodes that are descendants of this node. If True, creates a clone of the complete tree below this node. If False, clones this node and its attributes only.
cloneRoot [out, retval]
A newly created clone node.

C/C++ Return Values

S_OK
The value returned if successful.
E_INVALIDARG
The value returned if the cloneRoot parameter is Null.

Remarks

The cloned node has the same property values as this node for the following properties: nodeName property, nodeValue property, nodeType property, parentNode property, ownerDocument property, and, if it is an element, attributes property. The value of the clone's childNodes property depends on the setting of the deep flag parameter.

Note   If the node is the DOMDocument node, it is safer to clone the document using the save method, as follows.
doc.save(doc2);

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

nodeName Property | nodeValue Property | nodeType Property | parentNode Property | ownerDocument Property | attributes Property | childNodes Property

Applies to: IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText