Navigating the Node List

MSXML 5.0 SDK

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

Navigating the Node List

You can retrieve a specific member of the node list by using the item method. The item method takes a number corresponding to a node's position within the node list. To retrieve the first node in the node list, call item(0). It is also possible to navigate through the node list using the nextNode method, which returns the next node in the node list.

The following Microsoft® JScript® code performs the same task as the code in the previous topic.

currentNode = elem1.childNodes.nextNode;
while (currentNode != null) {
  if (currentNode.text == "hello world")
    helloWorldNode = currentNode;
  currentNode = elem1.childNodes.nextNode;
  }

This is the same example in Microsoft Visual Basic® Scripting Edition (VBScript).

Set currentNode = elem1.childNodes.nextNode
Do While currentNode <> null
  If currentNode.text = "hello world" Then
    helloWorldNode = currentNode
  End If
  currentNode = elem1.childNodes.nextNode
Loop

The currentNode is modified using the node returned by elem1.childNodes.nextNode. The initial position of the node list is defined to be before the first node. Therefore, the first time nextNode is called on the node list, the first node is returned. The code tests for currentNode != null. If the current node is the last node in the list or the node list has no members, nextNode returns null.

See Also

Gathering Information About the Node List | item Method (IXMLDOMNodeList) | nextNode Method (IXMLDOMNodeList)