Accessing the Document Tree

MSXML 5.0 SDK

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

Accessing the Document Tree

You can access the tree for an XML document either by beginning at the root element and moving from node to node or by querying for a specific node or nodes. You can navigate to the root element of the document by using the documentElement property. This property returns the root element as an IXMLDOMNode.

The following books.xml file is the same as the file used in the document map.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="show_book.xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<!--catalog last updated 2000-11-01-->
<catalog xmlns="http://www.example.com/catalog/">
  <book id="bk101">
     <author>Gambardella, Matthew</author>
     <title>XML Developer's Guide</title>
     <genre>Computer</genre>
     <price>44.95</price>
     <publish_date>2000-10-01</publish_date>
     <description><![CDATA[An in-depth look at creating applications with XML, using <, >,]]> and &amp;.</description>
  </book>
  <book id="bk109">
     <author>Kress, Peter</author>
     <title>Paradox Lost</title>
     <genre>Science Fiction</genre>
     <price>6.95</price>
     <publish_date>2000-11-02</publish_date>
     <description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description>
  </book>
</catalog>

You can walk the tree to the first level of elements contained by the root using Microsoft® Visual Basic® Scripting Edition (VBScript).

Dim root
Dim xmlDoc
Dim child
 
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 to the XML document's root element, COLLECTION:
   Set root = xmlDoc.documentElement
   'Walk from the root to each of its child nodes:
   For Each child In root.childNodes
        MsgBox child.text
   Next
End If

When run, this example displays the text of each of the root's child nodes (book elements) as a separate text message.

You can use the following methods to navigate to a specific node or nodes deep within the tree.

  • nodeFromID method

    The nodeFromID method takes a unique ID, as identified by an XML schema or document type definition (DTD), and returns the node corresponding to that specific ID.

  • getElementsByTagName method (DOMDocument)

    The getElementsByTagName method takes a string representing a specific tag name and returns all element nodes with this tag name.

You can use the following code to retrieve all element nodes named <AUTHOR> in books.xml.

Dim ElemList
Dim xmlDoc
Set xmlDoc = CreateObject("Msxml2.DOMDocument.5.0")
xmlDoc.async = False
xmlDoc.load("c:\xml\books.xml")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set ElemList = xmlDoc.getElementsByTagName("author")
   For i=0 To (ElemList.length -1)
      MsgBox ElemList.item(i).xml
   Next
End If

The selectNodes method of the IXMLDOMNode object offers a more flexible way of accessing nodes using XML Path Language (XPath).

See Also

Document Map | documentElement Property | IXMLDOMNode | selectNodes Method