   
selectSingleNode()
Is a member of:
|
DOMDocument
XMLDOMNode
XMLDOMAttribute
XMLDOMCDATASection
XMLDOMComment
XMLDOMDocumentFragment
XMLDOMDocumentType
XMLDOMElement
XMLDOMEntity
XMLDOMEntityReference
XMLDOMNotation
XMLDOMProcessingInstruction
XMLDOMText
XTLRuntime
|
Syntax
Set objXMLDOMNode = objXMLDOMElement.selectSingleNode(strXQLQuery)
Remark
Once again, as in the selectNodes() method, selectSingleNode() uses XQL to find the required Node. However, it only returns a single Node, XMLDOMNode object, which is the first Node that matches the pattern if it's found. If no match is found, it returns NULL.
Example
The selectNodes() and selectSingle-Node() methods can also be used to search for Nodes that have certain attribute values. As we mentioned, you will need to do a bit of research to work out how to work with XQL, which we do not cover in detail here.
In the following example, we iterate through all the childNodes in the documentElement collection. We only want to find the NAME element in each PERSON element.
Dim objDOMDocument As DOMDocument
Dim objXMLDOMNode As IXMLDOMNode
Dim objElement As IXMLDOMElement
Set objDOMDocument = New DOMDocument
objDOMDocument.async = False
objDOMDocument.Load "http://localhost/xmlcode/people2.xml"
For Each objXMLDOMNode In objDOMDocument.documentElement.childNodes
Set objElement = objXMLDOMNode.selectSingleNode("NAME")
' do something with this name element
Next
Look at getElementsByTagName(), which does almost the same thing as this method. The beauty of this method, however, is that it returns a single Node instead of an XMLDOMNodeList. Therefore, this method is easier to work with, as you don't need to iterate through a NodeList collection to just get the one value out of it.    
This manuscript is an abridged version of a chapter
from the Manning
Publications book XML
Programming with VB and ASP. This chapter looks at the Microsoft DOM objects. NOTE: Most images have been removed to increase speed and many of the code comments have also been removed for presentation. Please purchase the book to enjoy the full experience of all the chapters with images and code comments!
|