Home > Language Reference > XML Functions > AppendXMLNode

AppendXMLNode

The AppendXMLNode function adds a node in the specified XML file.

Syntax

ret = AppendXMLNode(<XML_filename>,<XML_path>,<tagname>)

Parameters

<XML_filename>. string, name of the XML file.

<XML_path>. string, XML path (case insensitive). See Remarks below to know how is built such a path.

<tagname>, string, value of the tag to be added.

Return value

ret, optional integer return value. If the node has been successfully added to the XML file, the return value is 0. If the function fails, the return value is a nonzero value (see XML Functions Error Codes).

Remarks

A XML path is a list of node descriptors separated by the \ character. If <xml_path> is empty, AppendXMLNode creates the root node.

The XML path can include tagnames such as:
tagname[attributename1='value']
The tagnames are strings without double quotes, they cannot contain spaces or reserved characters. 

The XML path can include attribute names. They are strings without double quotes, OR it can be the reserved keyword <text> or the reserved keyword <index>. <text> is used to specify the node inner text, <index> is used to specify the node index (first one starts at 1) when more nodes with the same tag and attributes exist within the same parent. Several attributes can be listed with the syntax tagname[attributename1='value1', attributename2='value2'], for example :
If the XML file contains:
< bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
the <XML_path> can be "bookstore\book[genre='autobiography']" or "bookstore\book[publicationdate='1981']" or "bookstore\book[publicationdate='1981',genre='autobiography']"

See also

EnumXMLAttributes
EnumXMLChildren
GetXMLAttribute
SetXMLAttribute

Example

'This code will generate the XML file as below:
file$ = "c:\wttest\test.xml"
create(file$)
appendxmlnode(file$, "", "root")
appendxmlnode(file$, "root", "book")
appendxmlnode(file$, "root", "book")
appendxmlnode(file$, "root\book", "Mark_Twain")
appendxmlnode(file$, "root\book", "Mark_Twain")
appendxmlnode(file$, "root\book[<index>=2]", "Portugalia")
setxmlattribute(file$, "root\book\Mark_Twain", "<text>", "Misc description")
setxmlattribute(file$, "root\book\Mark_Twain[<index>=2]", "year", "1987")
setxmlattribute(file$, "root\book\Mark_Twain[year='1987']", "<text>", "Another misc description")


Resulting XML file:
<root>
 <book>
  <Mark_Twain>Misc description</Mark_Twain>
  <Mark_Twain year="1987">Another misc description</Mark_Twain>
 </book>
 <book>
  <Portugalia />
 </book>
</root>