IXMLDOMElement::getElementsByTagName

banner art

Previous Next

IXMLDOMElement::getElementsByTagName

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

Syntax

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

Parameters

bstrTagName

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

ppResultList

[out] Pointer to a pointer to an IXMLDOMNodeList 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 Values

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

Remarks

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.

Example Code

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.
        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