IXMLDOMNode::replaceChild
Previous | Next |
IXMLDOMNode::replaceChild
The replaceChild method replaces the specified old child node with the supplied new child node.
Syntax
HRESULT replaceChild( IXMLDOMNode* pNewChild, IXMLDOMNode* pOldChild, IXMLDOMNode** ppOutOldChild );
Parameters
pNewChild
[in] Pointer to an IXMLDOMNode interface containing the address of the new child that is to replace the old child. If this parameter is NULL, pOldChild is removed without a replacement.
pOldChild
[in] Pointer to an IXMLDOMNode interface containing the address of the old child that is to be replaced by the new child.
ppOutOldChild
[out] Pointer to a pointer to an IXMLDOMNode interface containing the old child that is replaced. 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
When replacing an old child node with a new child node, the new child must be a valid child type for the existing parent node. The following tables list which child node types are valid and not valid for the parent node types specified.
The following table lists possible child node types for the parent node type NODE_ATTRIBUTE.
Child node type | Description |
NODE_ATTRIBUTE NODE_COMMENT NODE_ELEMENT |
Not valid. Returns an error. These node types cannot be children of an attribute. |
NODE_PROCESSING_INSTRUCTION | Not valid. Returns an error. This node type either cannot be a child of an attribute node or it is read-only. |
NODE_TEXT | Replaces the specified pOldChild with the supplied pNewChild and returns pOldChild. |
The following table lists possible child node types for the parent node type NODE_DOCUMENT.
Child node type | Description |
NODE_ATTRIBUTE NODE_DOCUMENT NODE_TEXT |
Not valid. Returns an error. These nodes cannot be children of a document node. |
NODE_COMMENT NODE_PROCESSING_INSTRUCTION |
Replaces the specified pOldChild with the supplied pNewChild and returns pOldChild. |
NODE_ELEMENT | Replaces pOldChild with pNewChild and returns pOldChild. By definition, an XML document (the document node) can have only a single child element. Therefore, an error is returned if the document node already has a child element. |
The following table lists possible child node types for the parent node type NODE_ELEMENT.
Child node type | Description |
NODE_ATTRIBUTE NODE_DOCUMENT |
Not valid. Returns an error. These node types cannot be children of an element node. |
NODE_COMMENT NODE_ELEMENT NODE_PROCESSING_INSTRUCTION NODE_TEXT |
Replaces the specified pOldChild with pNewChild and returns pOldChild. |
Example Code
The following example retrieves a pointer to an IXMLDOMNode interface, and replaces the specified child node with a new node.
#include "wmsserver.h" #include <atlbase.h> // Includes CComVariant and CComBSTR. // Declare variables. IWMSServer* pServer; IXMLDOMDocument* pPlaylist; IXMLDOMElement* pXMLElement; IXMLDOMElement* pXMLElementNew; IXMLDOMNodeList* pXMLNodeList; IXMLDOMNode* pXMLNodeCur; IXMLDOMNode* pXMLNodeRpl; IXMLDOMNode* pXMLNodeOld; HRESULT hr; VARIANT_BOOL bIsSuccessful; CComVariant varFile; CComBSTR bstrName; // 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. varFile = "c:\\wmpub\\wmroot\\simple.wsx"; hr = pPlaylist->load(varFile, &bIsSuccessful); if (FAILED(hr)) goto EXIT; if (bIsSuccessful) { // Retrieve a pointer to an IXMLDOMElement interface. hr = pPlaylist->get_documentElement(&pXMLElement); if (FAILED(hr)) goto EXIT; // Create a new element. bstrName = "seq"; hr = pPlaylist->createElement(bstrName, &pXMLElementNew); if (FAILED(hr)) goto EXIT; // Retrieve a list of child nodes. hr = pXMLElement->get_childNodes(&pXMLNodeList); if (FAILED(hr)) goto EXIT; // Retrieve the second child node. hr = pXMLNodeList->get_item(1, &pXMLNodeCur); if (FAILED(hr)) goto EXIT; // Retrieve a list of child nodes from the current node. hr = pXMLNodeCur->get_childNodes(&pXMLNodeList); if (FAILED(hr)) goto EXIT; // Retrieve the first child of the current node. hr = pXMLNodeList->get_item(0, &pXMLNodeRpl); if (FAILED(hr)) goto EXIT; // Replace the child of the current node with // the newly created node. hr = pXMLNodeCur->replaceChild(pXMLElementNew, pXMLNodeRpl, &pXMLNodeOld); 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 |