Microsoft XML SDK 2.6 - XML Reference

getElementsByTagName Method (DOMDocument)

Returns a collection of elements that have the specified name.

objXMLDOMNodeList =
    oXMLDOMDocument.getElementsByTagName(tagName)

Parameters

tagName
String specifying the element name to find. The tagName "*" returns all elements in the document.

Return Value

Object. Points to a collection of elements that match the specified name.

Remarks

The elements in the collection are returned in the order in which they would be encountered in a preorder traversal of the document tree. In a preorder traversal, the parent root node is visited first and each child node from left to right is then traversed.

The returned XMLDOMNodeList object is live and immediately reflects changes to the nodes that appear in the list.

More complex searches can be performed using the selectNodes method, which may also be faster in some cases.

Example

The following example creates an XMLDOMNodeList object using the DOMDocument object's getElementsByTagName method, and then displays all of the elements with the desired tag name:

Dim ElemList
Dim xmlDoc
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.async = False
xmlDoc.load("c:\books.xml")
Set objNodeList = xmlDoc.getElementsByTagName("AUTHOR")
For i=0 To (objNodeList.length -1)
  MsgBox (objNodeList.item(i).xml)
Next

See Also

Using Collections

Applies To: DOMDocument Object