Microsoft XML 2.5 SDK


 

IXMLDOMDocument::getElementsByTagName Method

[This is preliminary documentation and subject to change.]

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 Value

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

The returned IXMLDOMNodeList 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
{
   // Initialise 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 after done with it