The getElelmentsByTagName method retrieves a list of all descendant elements that match the supplied name.

HRESULT getElementsByTagName(
BSTR bstrTagName,
IXMLDOMNodeList** ppResultList



[in] BSTR containing the element name to find. The string "*" matches all elements of this element.


[out] Pointer to a pointer to an IXMLDOMNodeListIXMLDOMNodeList Interface containing all the 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 Value

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


Elements appear in the order encountered in a preorder traversal of this element's tree.

A pointer to an IXMLDOMNodeList interface is retrieved even if there are no matches. In this case, the length of the list will be set to zero.

The IXMLDOMNodeList interface is live and immediately reflects changes to the nodes that appear in the list.


The following example creates an IXMLDOMNodeList object by using the IXMLDOMElement.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;
IXMLDOMElement*       pXMLElement;
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 a pointer to an IXMLElement interface.
    hr = pPlaylist->get_documentElement(&pXMLElement);
    if (FAILED(hr)) goto EXIT;

    // Retrieve all nodes with the node name "media".
    bstrName = "media";
    hr = pXMLElement->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.

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


Header: wmsserver.h.

Library: WMSServerTypeLib.dll.

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

See Also


IXMLDOMElement Interface

IXMLDOMNodeList Interface


XML DOM Methods (C++)