XML

XMLDOMNodeList Object

The XMLDOMNodeList object is a collection of node objects. It is primarily used to iterate through the element nodes. This object implements the IXMLDOMNodeList interface. The IXMLDOMNodeList interface reflects the current state of the nodes in the document, so a change in the nodes will be immediately reflected in the object. The property and methods of IXMLDOMNodeList are as follows:

IXMLDOMNodeList Property

Name Description
length Returns the number of nodes that are contained in the node list.

IXMLDOMNodeList Methods

Name Description
item (index) Returns the node located at position index in the node list. The first node is indexed as 0.
nextNode()* Returns the next node object in the node list. If there are no more nodes, it returns null.
reset()* Resets the pointer so that it points before the first node element.

For an example of the IXMLDOMNodeList interface, you can add an attribute to the XML document and another command button to the frmDOMTest form. To do so, follow these steps:

  1. Open the XML document Books.xml and change the title element to the following:

  2.   <title language="English">Number, the Language of Science
      </title>
    
  3. Add another command button to the frmDOMTest form called cmdNodeList with the caption NodeList.
  4. Add the following code to the click event handler of the cmdNodeList button:

  5.   Private Sub cmdNodeList_Click()
          Dim objNodeList As IXMLDOMNodeList
          Dim objXMLDoc As DOMDocument
          Set objXMLDoc = New DOMDocument
          objXMLDoc.async = False
          objXMLDoc.Load ("c:\Books.xml")
          Set objNodeList = _
               objXMLDoc.documentElement.firstChild.childNodes
          Debug.Print "The second item's basename is: " & _
               objNodeList.Item(2).baseName
          Debug.Print "The number of nodes are: " & objNodeList.length
          Debug.Print "The first node xml is: " & vbCrLf & _
               objNodeList.nextNode.xml
          Debug.Print "The second node xml is: " & _
               objNodeList.nextNode.xml
          Debug.Print "The third node xml is: " & _
              objNodeList.nextNode.xml
              objNodeList.Reset
          Debug.Print "After reset, the first node xml is: " & _
              vbCrLf & objNodeList.nextNode.xml
          Dim intNodeCounter As Integer
          For intNodeCounter = 0 To objNodeList.length - 1
              Debug.Print "The " & "xml of node" & _
              Str(intNodeCounter + 1) & " is: " & vbCrLf & _
              objNodeList.Item(intNodeCounter).xml
          Next
          Set objNodeList = Nothing
          Set objXMLDoc = Nothing
      End Sub
    

Notice that, once again, we first get a reference to the document object. Once we have this reference, we can get a reference to the IXMLDOMNodeList interface. Then we use the item, nextNode, and reset methods of the IXMLDOMNodeList interface to navigate the document. Last, we print all the nodes in the collection with a loop. When you run this updated application and click the NodeList button, the results are as follows:

  The second item's basename is: price
  The number of nodes are: 4
  The first node xml is:
  <title language="English">Number, the Language of Science</title>
  The second node xml is: <author>Danzig</author>
  The third node xml is: <price>5.95</price>
  After reset, the first node xml is:
  <title language="English">Number, the Language of Science</title>
  The xml of node 1 is:
  <title language="English">Number, the Language of Science</title>
  The xml of node 2 is:
  <author>Danzig</author>
  The xml of node 3 is:
  <price>5.95</price>
  The xml of node 4 is:
  <quantity>3</quantity>

Notice that the attribute node was not included in the results. We will need to use the IXMLDOMNamedNodeMap interface to get a reference to attribute nodes.

by BrainBellupdated
Advertisement: