xmlAppendChild()
Adds a new child node into the DOM tree as child of given node. Optionally the node's value can be set.
Synopsis
int xmlAppendChild(unsigned doc, int node, int nodeType [, string value]);
Parameters
Parameter | Description |
---|---|
doc | Document ID. Reference to the whole XML document (return value from xmlNewDocument()). |
node | Child node ID. |
nodeType | Node type. See constants below. |
value |
Optional parameter. Value of the child node. |
Return value
If an error occurs, -1 is returned. Otherwise, it returns the node identifier of the newly created node.
Description
Adds a new child node into the DOM tree as child of given node. Optionally the node's value can be set.
When the node is passed as -1, the child node will be created as root node (child node of the document).
The entered node has one of the following node type constants:
Constant |
XML_ATTRIBUTE_NODE |
XML_CDATA_SECTION_NODE |
XML_COMMENT_NODE |
XML_DOCUMENT_NODE |
XML_DOCUMENT_FRAGMENT_NODE |
XML_DOCUMENT_TYPE_NODE |
XML_ELEMENT_NODE |
XML_ENTITY_NODE |
XML_ENTITY_REFERENCE_NODE |
XML_NOTATION_NODE |
XML_PROCESSING_INSTRUCTION_NODE |
XML_TEXT_NODE |
EXAMPLe
The example creates a new document through xmlNewDocument() and adds new nodes through xmlAppendChild() as well as prints the child nodes through xmlChildNodes().
#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 } |
Assignment
Availability
CTRL. In every script where the "CtrlXml" extension (#uses "CtrlXml") is used.
See also
xmlCloseDocument(), xmlDocumentFromFile(), xmlDocumentFromString(), xmlDocumentToFile(), xmlDocumentToString(), xmlElementAttributes(), xmlFirstChild(), xmlNewDocument(), xmlNextSibling(), xmlNodeName(), xmlNodeType(), xmlNodeValue(), xmlParentNode(), xmlSetElementAttribute(), xmlSetNodeValue()