IXMLDOMDocument::get_documentElement

The get_documentElement method retrieves the root element of the document.

HRESULT get_documentElement(
IXMLDOMElement** ppDOMElement
);

Arguments

ppDOMElement

[out] Pointer to a pointer to an IXMLDOMElementIXMLDOMElement Interface that represents the single element representing the root of the XML document tree. It returns NULL if no root exists. 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.

Remarks

This method retrieves a pointer to a pointer of the IXMLDOMElement interface that represents the root of the XML document tree. It returns NULL if no root exists.

To precisely place the node within the children of the document, call the IXMLDOMNode::insertBefore method.

Example

The following example retrieves a pointer to an IXMLDOMElement interface and sets it to the root element of the document by using the get_documentElement method. It then traverses the document tree.

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

// Declare variables.
IWMSServer*           pServer;
IXMLDOMDocument*      pPlaylist;
IXMLDOMElement*       pXMLElement;
IXMLDOMNode*          pXMLNodeA;
IXMLDOMNode*          pXMLNodeB;
IXMLDOMNodeList*      pXMLNodeList;
IXMLDOMNamedNodeMap*  pXMLNodeMap;

HRESULT               hr;
CComVariant           varNodeValue;
CComVariant           varValue;
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 the IXMLDOMElement interface.
    hr = pPlaylist->get_documentElement(&pXMLElement);
    if (FAILED(hr)) goto EXIT;

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

    // Display the node value from every attribute in the 
    // child nodes.
    for (int i = 0; i < lCount; i++)
    {
        hr = pXMLNodeList->get_item(i, &pXMLNodeA);
        if (FAILED(hr)) goto EXIT;
        hr = pXMLNodeA->get_attributes(&pXMLNodeMap);
        if (FAILED(hr)) goto EXIT;
        hr = pXMLNodeMap->get_item(0, &pXMLNodeB);
        if (FAILED(hr)) goto EXIT;
        hr = pXMLNodeB->get_nodeValue(&varNodeValue);
        if (FAILED(hr)) goto EXIT;

        // Release temporary COM objects.
        pXMLNodeA->Release();
        pXMLNodeMap->Release();
        pXMLNodeB->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

Reference

IXMLDOMDocument Interface

IXMLDOMElement Interface

Concepts

XML DOM Methods (C++)