Appends a new child node as the last child of the node.
JScript Syntax
var objXMLDOMNode = oXMLDOMNode.appendChild(newChild);
Parameters
- newChild
-
An object. Address of the new child node to be appended at the end of the list of children belonging to this node.
Return Value
An object. Returns the new child node successfully appended to the list.
Example
The following script example creates a new IXMLDOMNode object, and then uses the appendChild method to append it to the document's list of children. This example uses the resource file listed later in this topic.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
var root;
var newNode;
xmlDoc.async = false;
xmlDoc.load("appendChild.xml");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
WScript.Echo("You have error " + myErr.reason);
} else {
root = xmlDoc.documentElement;
WScript.Echo("Before appendChild:\n" + root.xml + "\n");
newNode = xmlDoc.createNode(1, "newChild", "");
root.appendChild(newNode);
WScript.Echo("After appendChild:\n" + root.xml);
}
Visual Basic Syntax
Set objXMLDOMNode = oXMLDOMNode.appendChild(newChild)
Parameters
- newChild
-
An object. The address of the new child node to be appended at the end of the list of children belonging to this node.
Return Value
An object. Returns the new child node successfully appended to the list.
Example
The following Microsoft® Visual Basic® example creates a new IXMLDOMNode object, and then uses the appendChild method to append it to the document's list of children. This example uses the resource file listed later in this topic.
Dim xmlDoc As New Msxml2.DOMDocument30
Dim root As IXMLDOMElement
Dim newNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.load App.Path & "\appendChild.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox("You have error " & myErr.reason)
Else
Dim str As String
Set root = xmlDoc.documentElement
str = "Before appendChild:" & vbCrLf & root.xml & vbCrLf
Set newNode = xmlDoc.createNode(1, "newChild", "")
root.appendChild newNode
str = str & "After appendChild:" & vbCrLf & root.xml
MsgBox str
End If
Resource File
The JScript and Visual Basic examples use the following file.
XSLT File: appendChild.xml
<?xml version='1.0'?>
<root>
<firstChild/>
</root>
Output
The JScript and Visual Basic examples output the following in a message box.
Before appendChild:
<root>
<firstChild/>
</root>
After appendChild:
<root>
<firstChild/>
<newChild/></root>
C/C++ Syntax
HRESULT appendChild(
IXMLDOMNode *newChild,
IXMLDOMNode **outNewChild);
Parameters
- newChild[in]
-
The address of the new child node to be appended to the end of the list of children of this node.
- outNewChild[out, retval]
-
The new child node successfully appended to the list. If Null, no object is created.
Return Values
- S_OK
-
The value returned if successful.
- E_INVALIDARG
-
The value returned if the newChild parameter is Null.
- E_FAIL
-
The value returned if an error occurs.
Remarks
Using this method is equivalent to calling insertBefore(newChild, null). For more information, see insertBefore Method.
If the newChild parameter has an existing parent, the node is automatically removed from that parent before being inserted into its new location.
A node retains its namespace information even when moved. Moving a node does not create a namespace declaration; declarations are added when retrieving the XML source (through the save method or the xml property) to ensure that all namespaces are properly declared.
When inserting a node tree under another node that has a different owner document, the ownerDocument property for each inserted node is changed to match the owner document of its new parent.
When moving a node tree to another document, the content of all entity reference nodes contained therein is updated to conform to the new document. If the new document does not declare an entity that was moved into it, the entity reference will have no children, and the old content is removed. Existing references to nodes under the entity reference are still valid, but the node whose parent previously was the entity reference now has a null parent.
MSXML 4.0, 5.0, and 6.0 only validate additions to the DOM when the user explicitly calls validate Method. This means that you do not have to add nodes to the tree in the same order as they are defined in the schema. The implication is that there may be intermediate states between calls to validate where the DOM is invalid against the schema.
Versioning
Implemented in: MSXML 3.0 and later
Applies to
See Also