XMLInsert Examples (Magic xpa 4.x)
<?xml version="1.0"?>
<order id="123">
<issued_by>
<address>
<street>Somewhere</street>
<city>Nowhere</city>
</address>
</issued_by>
<item cat_num="2145451544">
<price>99.99</price>
<amount>2</amount>
</item>
<item cat_num="1384325456">
<price>19.99</price>
<amount>10</amount>
</item>
</order>
Given that the first I/O device of the current task points to an XML file, consider the following expression:
XMLInsert (0, 1, '/order/issued_by/name','','John Smith')
This example adds a new XML element name as the last element inside the issued_by type. The issued_by section appears as:
<issued_by>
<address>
<street>Somewhere</street>
<city>Nowhere</city>
</address>
<name>John Smith</name>
</issued_by>
The return value is 1.
To add a new state element after the city element, use:
XMLInsert (0,1, '/order/issued_by/address/state','', 'Some state', 'A', 'city')
The issued_by section now appears as:
<issued_by>
<name>John Smith</name>
<address>
<street>Somewhere</street>
<city>Nowhere</city>
<state>Some state</state>
</address>
<name>John Smith</name>
</issued_by>
The return value is 1.
To add an attribute identification to an existing element name, use:
XMLInsert (0, 1, '/order/issued_by/name','ID','1')
This example inserts a new XML attribute to the specified element name. The issued_by section appears as:
<issued_by>
<address>
<street>Somewhere</street>
<city>Nowhere</city>
<state>Some state</state>
</address>
<name ID="1">John Smith</name>
</issued_by>
The return value is 0.
To add an attribute type to the second item, use:
XMLInsert (0, 1, '/order/item[2]', 'type', 'standard')
The function returns 0.
XMLInsert (0, 1, '/order/item[3]','','')
This example fails and returns Error Code -11 – Invalid Path, Non-valid Index – because an element is inserted and an index is specified for the rightmost element in the element path.