Share via


IXMLDOMNode::appendChild

banner art

Previous Next

IXMLDOMNode::appendChild

The appendChild method appends the supplied new child as the last child of the node.

Syntax

  HRESULT appendChild(
 IXMLDOMNode* pNewChild, 
 IXMLDOMNode** ppOutNewChild
);

Parameters

pNewChild

[in] Pointer to an IXMLDOMNode interface containing the address of the new child node to be appended at the end of the list of children belonging to this node.

ppOutNewChild

[out] Pointer to a pointer to an IXMLDOMNode interface representing the new child node appended to the list. If this parameter is NULL, no object is created. 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

If pNewChild has an existing parent, the node is automatically removed from that parent before being inserted into its new location.

When inserting a node tree under another node that has a different owner document, the pointer to the IXMLDOMDocument interface that is retrieved by the get_ownerDocument method for each inserted node is changed to match the owner document of its new parent.

When moving a node tree to another document, the content of all entity reference nodes contained therein is updated to conform to the new document. If the new document does not declare an entity that was moved into it, the entity reference will have no children, and the old content is removed. Existing references to nodes under the entity reference are still valid, but the node whose parent was previously the entity reference now has a null parent.

This is equivalent to calling insertBefore (objNewChild, NULL). For more information, see the IXMLDOMNode::insertBefore method.

Example Code

The following example retrieves a pointer to an IXMLDOMNode interface, and then uses the appendChild method to append it to the document's list of children.

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

// Declare variables.
IWMSServer*           pServer;
IXMLDOMDocument*      pPlaylist;
IXMLDOMElement*       pXMLElement;
IXMLDOMNode*          pXMLNewChild;
IXMLDOMNode*          pXMLOutNewChild;

HRESULT               hr;
CComBSTR              bstrName;
CComBSTR              bstrNamespaceURI;
CComVariant           varValue;
CComVariant           varNodeType;
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;

    // Create an new node with the following parameters and 
    // append it to the list of child nodes for the document.
    varNodeType = NODE_ELEMENT;
    bstrName = "media";
    bstrNamespaceURI = "";
    hr = pPlaylist->createNode(varNodeType, bstrName, bstrNamespaceURI,
                             &pXMLNewChild);
    if (FAILED(hr)) goto EXIT;
    hr = pXMLElement->appendChild(pXMLNewChild, &pXMLOutNewChild);
    if (FAILED(hr)) goto EXIT;
}

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