Microsoft XML 2.5 SDK


 

Using the XMLDOMNode Object

[This is preliminary documentation and subject to change.]

The XMLDOMNode object is the principle object within the DOM. The XMLDOMDocument object is itself an XMLDOMNode object, as are the members of node lists and named node maps. The following topics discuss how to use the XMLDOMNode object:

Gathering Information About the Node

The following properties allow you to get information concerning the node:

You can access these properties to get information concerning the type of the node, the name of the node, whether the node has any children, whether the node is explicitly specified in the XML file or implied within the Document Type Definition (DTD) or XML Schema, the namespace to which the node belongs, whether the node has been parsed, and the string representation of the node.

The following sample code tests to see if the node is of type "element." If the node is of type "element," the string representation of the node is loaded into a document object through the loadXML method.

if (xmlNode.nodeTypeString == "element")
  XMLDoc.loadXML(xmlNode.xml)

Getting and Setting the Data Within a Node

The data marked up within the XML file is exposed in the DOM as node values. These values might be the value of an attribute, for instance, or the text within an XML element. There are three ways to access the value of a node. The nodeValue property gives you access to values of certain nodes. The nodes that contain a value that can be accessed through the nodeValue property are attributes, text nodes, comments, processing instructions, and CDATA sections. The following sample code sets the value of an attribute to "hello world."

newAttNode = XMLDoc.createAttribute("newAtt")
newAttNode.nodeValue = "hello world"

There are three ways to access text within an element node. You can:

Navigating the Tree

From the XMLDOMNode object, you can navigate directly to its parent node (parentNode), children (childNodes, firstChild, lastChild), siblings (previousSibling, nextSibling), or the document object to which the node belongs (ownerDocument). If the node is of type element, attribute, or entityReference, you can call the definition property to navigate to the schema definition of the node. In addition, if the node is of type element, processingInstruction, documentType, entity, or notation, you can navigate to the attributes on the node using the attributes property. The following sample code tests to see if the element "elem1" has any element children. If "elem1" has element children, it navigates from "elem1" to that element's first child. The text of that first child is then changed to "I'm the first child of elem1."

if (elem1.hasChildNodes == true && elem1.firstChild.nodeTypeString == "element")
  elem1.firstChild.text = "I'm the first child of elem1"

It is also possible to navigate to other nodes in the tree using selectNodes and selectSingleNode. These methods take an XSL Pattern as an argument and return the node or nodes that match that query. For more information on XSL Patterns, see XSL Patterns Reference.

Manipulating the Children of a Node

The XMLDOMNode object exposes four methods that allow you to manipulate the children of a node: appendChild, replaceChild, removeChild, and insertBefore. Each of these methods takes an XML node object as an argument. The following sample 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 somewhere else within 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 VBScript example loads Books.xml (see Accessing the Document Tree previously in this topic) and creates a new element, PAGES. Then, using a reference to the first child of a node 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("microsoft.xmldom")
xmlDoc.async = False
xmlDoc.load("c:\xmlinact\books.xml")
Set root = xmlDoc.documentElement
Set newElem = xmlDoc.createElement("PAGES")
Set refElem = root.childNodes.item(1).firstChild
root.childNodes.item(1).insertBefore newElem, refElem
MsgBox root.childNodes.item(1).xml

The next example uses the removeChild method to remove a child node:

Dim xmlDoc
Dim root
Dim oldChild
Set xmlDoc = CreateObject("microsoft.xmldom")
xmlDoc.async = False
xmlDoc.load("c:\books.xml")
Set root = xmlDoc.documentElement
Set oldChild = root.childNodes.item(1).firstChild
root.childNodes.item(1).removeChild oldChild
MsgBox root.childNodes.item(1).xml

Transforming a Node

The transformNode and transformNodeToObject methods can be used to transform an XML node into a string or object using XSL. For more information on transforming nodes using XSL, see Using the XSL Processor.