Returns a collection of elements that have the specified name.
var objXMLDOMNodeList = oXMLDOMDocument.getElementsByTagName(tagName);
tagName
value "*"
returns all elements in the document.An object. Points to a collection of elements that match the specified name.
The following script example creates an IXMLDOMNodeList
object using the DOMDocument
object's getElementsByTagName
method, and then displays all of the elements with the desired tag name.
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.5.0"); var objNodeList; xmlDoc.async = false; xmlDoc.load("books.xml"); if (xmlDoc.parseError.errorCode <> 0) { var myErr = xmlDoc.parseError; alert("You have error " + myErr.reason); } else { objNodeList = xmlDoc.getElementsByTagName("author"); for (var i=0; i<objNodeList.length; i++) { alert(objNodeList.item(i).xml); } }
Set objXMLDOMNodeList = oXMLDOMDocument.getElementsByTagName(tagName)
tagName
value "*"
returns all elements in the document.An object. Points to a collection of elements that match the specified name.
The following Microsoft® Visual Basic® example creates an IXMLDOMNodeList
object using the DOMDocument
object's getElementsByTagName
method, and then displays all of the elements with the desired tag name.
Dim xmlDoc As New Msxml2.DOMDocument50 Dim objNodeList As IXMLDOMNodeList xmlDoc.async = False xmlDoc.Load ("books.xml") If (xmlDoc.parseError.errorCode <> 0) Then Dim myErr Set myErr = xmlDoc.parseError MsgBox("You have error " & myErr.reason) Else Set objNodeList = xmlDoc.getElementsByTagName("author") For i = 0 To (objNodeList.length - 1) MsgBox (objNodeList.Item(i).xml) Next End If
HRESULT getElementsByTagName( BSTR tagName, IXMLDOMNodeList **resultList);
tagName
value"*"
returns all elements in the document.IXMLDOMDocument *pIXMLDOMDocument = NULL; wstring strFindText (_T("author")); IXMLDOMNodeList *pIDOMNodeList = NULL; IXMLDOMNode *pIDOMNode = NULL; long value; BSTR bstrItemText; HRESULT hr; try { // Initialize pIXMLDOMDocument (create a DOMDocument). // Load document. hr = pIXMLDOMDocument->getElementsByTagName( (TCHAR*)strFindText.data(), &pIDOMNodeList); SUCCEEDED(hr) ? 0 : throw hr; hr = pIDOMNodeList->get_length(&value); if(SUCCEEDED(hr)) { pIDOMNodeList->reset(); for(int ii = 0; ii < value; ii++) { pIDOMNodeList->get_item(ii, &pIDOMNode); if(pIDOMNode ) { pIDOMNode->get_text(&bstrItemText); ::MessageBox(NULL, bstrItemText,strFindText.data(), MB_OK); pIDOMNode->Release(); pIDOMNode = NULL; } } } pIDOMNodeList->Release(); pIDOMNodeList = NULL; } catch(...) { if(pIDOMNodeList) pIDOMNodeList->Release(); if(pIDOMNode) pIDOMNode->Release(); DisplayErrorToUser(); } // Release pIXMLDOMDocument when finished with it.
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 IXMLDOMNodeList
object is live and immediately reflects changes to the nodes that appear in the list.
The getElementsByTagName method simulates the matching of the provided argument against the result of the tagName property of IXMLDOMElement. When executed, it does not recognize or support namespaces. Instead you should use the selectNodes method, which is faster in some cases and can support more complex searches.
To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button in the upper-left corner of the page.
IXMLDOMNodeList | selectNodes Method
Applies to: DOMDocument