Microsoft XML SDK 2.6 - XML Reference

IXMLDOMDocument::getElementsByTagName Method

Returns a collection of elements that have the specified name.

Visual Basic Syntax

Set objXMLDOMNodeList = oXMLDOMDocument.XMLDOMDocument(tagName)

C/C++ Syntax

HRESULT getElementsByTagName(
    BSTR tagName,
    IXMLDOMNodeList **resultList);

Parameters

tagName [in]
Element name to find. The tagname "*" returns all elements in the document.
resultList [out]
Address of a collection of elements that match the specified name.

C/C++ Return Values

Returns S_OK if successful, or an error code otherwise.

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 then each child node from left to right is traversed.

The returned DOMNodeList 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.

C/C++ Example

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.

See Also

IXMLDOMDocument Interface