Working with XML Document Nodes

As you already know, the XML DOM represents a tree-based hierarchy of nodes. An XML document can contain nodes of different types. For example, an XML document can include a document node that provides access to the entire XML document or one or more element nodes representing individual elements. Some nodes represent comments and processing instructions in the XML document, and others hold the text content of a tag. To determine the type of node, use the nodeType property of the IXMLDOMNode object. Node types are identified either by a text string or a constant. For example, the node representing an element can be referred to as NODE_ELEMENT or 1, while the node representing the comment is named NODE_COMMENT or 8. See the MSXML2 Library in the Object Browser (Figure 17-24 earlier in this chapter) for the names of other node types.

In addition to node types, nodes can have parent, child, and sibling nodes. The hasChildNodes method lets you determine if a DOMDocument object has child nodes. There's also a childNodes property with which it's quite simple to retrieve a collection of child nodes. Before you start looping through the collection of child nodes, it's a good idea to use the length property of the IXMLDOMNode to determine how many elements the collection contains.

The LearnAboutNodes procedure shown below will get you working with nodes programmatically in no time. The result of the procedure, as printed in the Immediate window, is shown following the procedure code.

The following exercise demonstrates how to experiment with XML document nodes.

1. Enter the following procedure in a module. Remember to add References to the MSXML type library and change the path of the XML document to point to the file location on your disk.

2. Run the LearnAboutNodes procedure in a step mode by pressing F8.

Sub LearnAboutNodes() ' Create an instance of the DOMDocument Dim xmldoc As MSXML2.DOMDocument30 Dim xmlNode As MSXML2.IXMLDOMNode

Set xmldoc = New MSXML2.DOMDocument30 xmldoc.async = False ' Load XML information from a file

' Be sure to change the file path to point to the location ' of the Courses.xml file on your computer xmldoc.Load ("C:\ExcelVBA2002\Chap17\Courses.xml") ' find out the number of child nodes in the document If xmldoc.hasChildNodes Then

Debug.Print "Number of Child Nodes: " & xmldoc.childNodes.Length ' iterate through the child nodes to gather information For Each xmlNode In xmldoc.childNodes

Debug.Print "Node Name: " & xmlNode.nodeName Debug.Print vbTab & "Type: " & xmlNode.nodeTypeString _

& "(" & xmlNode.nodeType & ")" Debug.Print vbTab & "Text: " & xmlNode.Text Next xmlNode End If End Sub

The LearnAboutNodes procedure prints to the Immediate window the information about child nodes found in the Courses.xml document. Notice that the text property of a node returns all the text from all the node's children in one string (see the text for the Courses node below):

Number of Child Nodes: 3 Node Name: xml

Type: processinginstruction(7) Text: version="1.0" Node Name: #comment Type: comment(8)

Text: <?xml-stylesheet type="text/xsl" href="Courses2.xsl"?> Node Name: Courses Type: element(l)

Text: Beginning VBA in Excel 2/3/2003 6 Intermediate

VBA in Excel 4/3/2003 8 Advanced VBA in Excel 6/3/2003 12

0 0


  • gianni mancini
    How to add text in xml node through vba?
    8 years ago

Post a comment