共用方式為


cloneNode Method

 

Clones a new node.

JScript 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.6.0");  
var root;  
var currNode;  
var MyNewNode;  
xmlDoc.async = false;  
xmlDoc.loadXML("<root><book/><AUTHOR/></root>")  
if (xmlDoc.parseError.errorCode != 0) {  
   var myErr = xmlDoc.parseError;  
   WScript.Echo("You have error " + myErr.reason);  
} else {  
   root = xmlDoc.documentElement;  
   currNode = root.childNodes.item(1);  
   MyNewNode = currNode.cloneNode(true);  
   root.appendChild(MyNewNode);  
   WScript.Echo(xmlDoc.xml);  
}  

Output

<root><book/><AUTHOR/><AUTHOR/></root>  

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.

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.

Versioning

Implemented in: MSXML 3.0 and MSXML 6.0

Applies to

IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | IXMLDOMDocument-DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText

See Also

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