Retrieving the First Matching Node

If all you want to do is retrieve the first node that meets the specified criterion, use the SelectSingleNode method of the DOMDocument object. As the argument of this method, specify the string representing the node you'd like to find. For example, the following procedure finds the first node that matches the criterion "//CompanyName" in the Shippers.xml file:

Sub SelectSingleNode()

Dim xmldoc As MSXML2.DOMDocument50 Dim xmlSingleNode As MSXML2.IXMLDOMNode

Set xmldoc = New MSXML2.DOMDocument50 xmldoc.async = False xmldoc.Load ("C:\Learn_XML\Shippers.xml") Set xmlSingleNode = xmldoc.SelectSingleNode("//CompanyName") If xmlSingleNode Is Nothing Then Debug.Print "No nodes selected."

Else

Debug.Print xmlSingleNode.Text End If

Set xmlDoc = Nothing End Sub

XML DOM provides a number of other methods that make it possible to pro-grammatically add or delete elements in the XML document tree structure. Covering all of the details of the XML DOM Object Model is beyond the scope of this chapter. When you are ready for more information on this subject, visit the following web link: http://www.w3.org/DOM/.

Note: When you run this procedure again you should see the following output:

Speedy Express United Package Airborne Express

Part V

0 0

Post a comment