Manipulating the Children of a Node

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Developer's Guide

Manipulating the Children of a Node

The IXMLDOMNode object exposes four methods that allow you to manipulate the children of a node. Each of these methods takes an IXMLDOMNode object as an argument.

The following code takes a new element that is created using createElement and adds that as the last child of elem1.

newChild = XMLDoc.createElement("last_child")
elem1.appendChild(newChild)

The appendChild method always adds the node to the end of the list of children. To insert the node elsewhere in the child node list, use the insertBefore method. This method takes two parameters: the node to be inserted and the node before which the new child is to be inserted.

The following Microsoft® Visual Basic® Scripting Edition (VBScript) example loads books.xml and creates a new element, pages. Then, using a reference to the first child of the first node contained by the document element as the reference point (refElem), it inserts the new element with the insertBefore method.

Dim xmlDoc
Dim root
Dim newElem
Dim refElem
Set xmlDoc = CreateObject("Msxml2.DOMDocument.5.0")
xmlDoc.async = False
xmlDoc.load("c:\xmlinact\books.xml")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   'create new element
   Set newElem = xmlDoc.createElement("pages")
   'start from the root element
   Set root = xmlDoc.documentElement
      'find the first child of the root element (using collection syntax)
Set firstElem=root.childNodes.item(1)
   'find the first child of the first child (using property syntax)
   Set refElem = firstElem.firstChild
   'insert our new pages element
   root.childNodes.item(1).insertBefore newElem, refElem
   'report the content of the first child of the root element
   MsgBox firstElem.xml
End If

The following is an example in Microsoft JScript®.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
xmlDoc.async = false;
xmlDoc.load("c:\\books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   //create new element
   var newElem = xmlDoc.createElement("pages");
   //start from the root element
   var root = xmlDoc.documentElement;
   //find the first child of the root element (using collection syntax)
   var firstElem=root.childNodes.item(1);
   //find the first child of the first child (using property syntax)
   var refElem = firstElem.firstChild;
   //insert our new pages element
   root.childNodes.item(1).insertBefore(newElem, refElem);
   //report the content of the first child of the root element
   alert(firstElem.xml);
}

The following VBScript example uses the removeChild method to remove a child node. Most of the code is similar to the preceding example, but this script removes an element rather than creating and adding one.

Dim xmlDoc
Dim root
Dim oldChild
Set xmlDoc = CreateObject("Msxml2.DOMDocument.5.0")
xmlDoc.async = False
xmlDoc.load("c:\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
   'find the first child of the root element (using collection syntax)
   Set firstElem=root.childNodes.item(1)
   Set oldChild = firstElem.firstChild
   firstElem.removeChild oldChild
   MsgBox firstElem.xml
End If

The following is an example in JScript.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0");
xmlDoc.async = false;
xmlDoc.load("c:\\books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   //start from the root element
   var root = xmlDoc.documentElement;
   //find the first child of the root element (using collection syntax)
   var firstElem=root.childNodes.item(1);
   //pick the element to remove
   var oldChild = firstElem.firstChild;
   //remove the element
   firstElem.removeChild (oldChild);
   //report the content of the first child of the root element
   alert(firstElem.xml);
}

See Also

Accessing the Document Tree