xmlReplaceNode()
Replaces an existing node in an XML document.
Synopsis
int xmlReplaceNode(unsigned docToReplace, unsigned nodeToReplace, unsigned
docNew, unsigned nodeNew);
Parameters
| Parameter | Description |
|---|---|
| docToReplace | The existing document to be replaced by the new document. The document ID is returned by xmlNewDocument(). |
| nodeToReplace | The node name (ID) of the node to be replaced by the new node. |
| docNew | The new document that replaces the original document. The document ID is returned by xmlNewDocument(). |
| nodeNew | The node name (ID) of the new node that replaces the existing node. |
Return value
Returns 0 if the removal operation succeeds.
Returns -1 if an error occurs.
Details
Replaces an existing XML document and an existing node within that document.
Example
This example creates a new document in the memory and adds nodes to it. It also sets attributes, sets a node value, and prints the document’s content.
The original document is then replaced with the newly created one. The first node in the original document is also replaced with the newly created node.
#uses "CtrlXml"
main()
{
dyn_uint nodes;
string nNameP, nName1,nName2, nName3, nName4, nName5;
unsigned docNum = xmlNewDocument();
//Create a new document
xmlAppendChild(docNum, -1, XML_COMMENT_NODE, "my fine new comment");
//Add a new node
int node = xmlAppendChild(docNum, -1, XML_ELEMENT_NODE, "Parent node");
//Add a new node
nNameP = xmlNodeName(docNum, node);
int PNode = xmlParentNode(docNum, node);
DebugN("Parent node:", nNameP);
xmlAppendChild(docNum,PNode, XML_ELEMENT_NODE, "First element");
nName1 = xmlNodeName(docNum, node);
DebugN("Node 1:", nName1);
int node2 = xmlAppendChild(docNum, -1, XML_ELEMENT_NODE, "Second element");
nName2 = xmlNodeName(docNum, node2);
DebugN("Node 2:", nName2);
int j = xmlSetElementAttribute(docNum, node2, "Attribute1", "Example Attribute");
//Set a new attribute
int node3 = xmlAppendChild(docNum, node2, XML_ELEMENT_NODE, "Third element");
nName3 = xmlNodeName(docNum,node3);
DebugN("Node 3:", nName3);
int i = xmlSetNodeValue(docNum, nName2, "And a node value");
//Set a new value
int node4 = xmlAppendChild(docNum, -1, XML_ELEMENT_NODE, "Fourth element");
nName4 = xmlNodeName(docNum, node4);
DebugN("Node 4:", nName4);
int node3 = xmlAppendChild(docNum, -1, XML_ELEMENT_NODE, "Fifth element");
xmlSetElementAttribute(docNum, node3, "Attribute2", "A second example attribute");
int k = xmlChildNodes(docNum, PNode, nodes);
DebugN("xmlChildNodes successful:", k, "List of the nodes:", nodes);
//Print the child nodes
unsigned docNew = xmlNewDocument();
int NewNode =xmlAppendChild(docNew, -1, XML_COMMENT_NODE, "My new node in the new DOC");
string nNamePanel;
//Add a new document and a new node
nNamePanel = xmlNodeName(docNew, NewNode);
int ParentNode = xmlParentNode(docNew, NewNode);
int retVal;
retVal = xmlReplaceNode(docNum, node,docNew,NewNode);
DebugN("xmlReplaceNode returns:", retVal,"New document:", xmlDocumentToString(docNew));
DebugN("xmlReplaceNode returns:", retVal,"Old document:", xmlDocumentToString(docNum));
}
Assignment
Availability
CTRL. In every script where the "CtrlXml" extension (#uses "CtrlXml") is used.
