IXMLDOMDocument::getElementsByTagName

banner art

Previous Next

IXMLDOMDocument::getElementsByTagName

The getElementsByTagName method retrieves a collection of elements that have the specified name.

Syntax

  HRESULT getElementsByTagName(
 BSTR bstrTagName,
 IXMLDOMNodeList** ppResultList
);

Parameters

bstrTagName

[in] BSTR containing the element name to find. The tag name "*" returns all elements in the document.

ppResultList

[out] Pointer to a pointer to an IXMLDOMNodeList interface containing a collection of elements that match the specified name. This method calls AddRef internally. To avoid memory leaks, you must call Release when you are finished using the interface.

Return Values

If the method succeeds, it returns S_OK. If it fails, it returns an HRESULT error code.

Remarks

The elements in the collection are retrieved 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 retrieved IXMLDOMNodeList interface is live and immediately reflects changes to the nodes that appear in the list.

Example Code

The following example retrieves a pointer to an IXMLDOMNodeList interface by using the getElementsByTagName method, and then retrieves all of the elements with the desired tag name.

#include "wmsserver.h"
#include <atlbase.h> // Includes CComVariant and CComBSTR.

// Declare variables.
IWMSServer*           pServer;
IXMLDOMDocument*      pPlaylist;
IXMLDOMNamedNodeMap*  pXMLNamedNodeMap;
IXMLDOMNodeList*      pXMLNodeList;
IXMLDOMNode*          pXMLAttNode;
IXMLDOMNode*          pXMLNode;

HRESULT               hr;
CComBSTR              bstrName;
CComVariant           varValue;
CComVariant           varNodeValue;
long                  lCount;
VARIANT_BOOL          bIsSuccessful;

// Initialize the COM library and retrieve a pointer
// to an IWMSServer interface.
hr = CoInitialize(NULL);
hr = CoCreateInstance(CLSID_WMSServer, NULL, CLSCTX_ALL, 
       IID_IWMSServer, (void**)&pServer);
if (FAILED(hr)) goto EXIT;

// Create the playlist object.
hr = pServer->CreatePlaylist(&pPlaylist);

// Load a sample playlist file.
varValue = "c:\\wmpub\\wmroot\\simple.wsx";
hr = pPlaylist->load(varValue, &bIsSuccessful);
if (FAILED(hr)) goto EXIT;

if (bIsSuccessful)
{
    // Retrieve all nodes with the node name "media".
    bstrName = "media";
    hr = pPlaylist->getElementsByTagName(bstrName, &pXMLNodeList);
    if (FAILED(hr)) goto EXIT;

    // Retrieve total number of nodes in the list.
    hr = pXMLNodeList->get_length(&lCount);
    if (FAILED(hr)) goto EXIT;

    // Iterate through the node list and display 
    // the node value of every attribute
    // for every node in the list.
    for (int i = 0; i < lCount; i++)
    {
        hr = pXMLNodeList->get_item(i, &pXMLNode);
        if (FAILED(hr)) goto EXIT;
        hr = pXMLNode->get_attributes(&pXMLNamedNodeMap);
        if (FAILED(hr)) goto EXIT;
        hr = pXMLNamedNodeMap->get_item(0, &pXMLAttNode);
        if (FAILED(hr)) goto EXIT;
        hr = pXMLAttNode->get_nodeValue(&varNodeValue);
        if (FAILED(hr)) goto EXIT;

        // Release temporary COM objects.
        pXMLNode->Release();
        pXMLNamedNodeMap->Release();
        pXMLAttNode->Release();
    }
}

EXIT:
    // TODO: Release temporary COM objects and uninitialize COM.

Requirements

Header: wmsserver.h.

Library: WMSServerTypeLib.dll.

Platform: Windows Server 2003 family, Windows Server 2008 family.

See Also

Previous Next