   
getElementsByTagName()
Is a member of:
|
DOMDocument
XMLDOMElement
|
set objXMLDOMNodeList = objDOMDocument.getElementsByTagName(tagname)
The parameter Tagname in the getElementsByTagName() signature is a string, specifying the element name to find. If you specify that the tagname is an asterisk ("*"), then all the elements are returned in the
DOMDocument.
The returned NodeList of this method is different from the nodeLists that we have dealt with so far. You normally work with a NodeList that returns a collection from the documentElement, such as:
set objXMLDOMNodeList = objDOMDocument.documentElement
This will return a NodeList collection that returns its child Nodes as shown in figure .
You can call getElementsByTagName() from the DOMDocument root, specifying that you want everything (*):
set objXMLDOMNodeList = objDOMDocument.getElementsByTagName("*")
In the above example, getElementsByTagName() returns a NodeList collection that has child Nodes as follows in figure :
This method returns a NodeList object in which each child Node is grouped by their element names, instead of being grouped by the normal TreeView effect.
To get a clearer view of what we are trying to explain, look in your Local Views in VB for the following examples. See the difference in the collections of the NodeList object by calling:
Dim objDOMDocument As DOMDocument
Dim objNodeList As IXMLDOMNodeList
Set objDOMDocument = New DOMDocument
objDOMDocument.async = False
objDOMDocument.Load "http://localhost/xmlcode/people2.dtd"
Set objNodeList = objDOMDocument.documentElement
Dim objDOMDocument As DOMDocument
Dim objNodeList As IXMLDOMNodeList
Set objDOMDocument = New DOMDocument
objDOMDocument.async = False
objDOMDocument.Load "http://localhost/xmlcode/people2.dtd"
Set objNodeList = objDOMDocument.getElementsByTagName("*")
¬ You can use this method when you want to get a specific Node, like NAME. However, take note that it returns a NodeList object; therefore, you need to iterate through the returned Nodes.
An example of this is when we want to put the NAME values of our element into a text box.
Set objNodeItems = objNode.getElementsByTagName("NAME")
txtName.Text = objNodeItems.Item(0).nodeTypedValue
For an easier method, look at the selectSingleNode() method later in this section.    
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!
|