XML Support

XML support is available in server automation scripts and libraries, allowing you to create and parse XML.

This is helpful if you want to use data formatted as XML. For example, you might want to use product or other related information you have already stored in XML format, instead of creating new entities and attributes.

Load XML from a String

Method

Copy
server.Xml.Load(string xml)

Loads the XML from the specified string.

Parameter

Copy
xml string

String containing the XML to load. The string is an XML formatted text.

Copy

var xml = '<Order></Order>';
var doc = server.Xml.Load(xml);

Catch an XML Load Error

When loading XML from a string, errors that might occur on XML load from a string are not automatically logged. To log any errors that might occur on XML schema load, use catch(err).

Copy

var xml = '<Order />';
try
{
    var doc = server.Xml.Load(xml);
}
catch(err)
{
    log(err);
}            

Run XPath Queries

You can perform XPath queries to navigate through nodes (elements, attributes) in an XML document.

Method

Copy
Query( xpath : string ) : Element[]
 

E.g.:

Copy

var xml = '<?xml version="1.0" encoding="utf-8" ?>
<Orders>
     <Order id="O1">
             
            <Product id="1" name="Nexus 5">
                <Price>400.00</Price>
                <Qty>1</Qty>
            </Product>
            <Product id="5" name="Wireless Charger">
                <Price>50.00</Price>
                <Qty>1</Qty>
            </Product>
     </Order>
     <Order id="O2">
            <Product id="2" name="IPhone">
                <Price>800.00</Price>
             <Qty>1</Qty>
            </Product>
            <Product id="5" name="Wireless Charger">
                <Price>50.00</Price>
                <Qty>1</Qty>
            </Product>
            </Order>
</Orders>';
var products = doc.Query("/Orders/Order/Product[@name='Wireless Charger']");
log(products[0]['id']);
log(products[1]['id']);            
 

Where:

  • <Orders> is the root element
  • Order is an element node
  • <Price> and <Qty> are child elements of <Product>
  • Wireless Charger is an attribute node

Run XPath Queries with Namespaces

You can perform XPath queries with namespaces to navigate through nodes (elements, attributes) in an XML document.

Method

Copy
Query( xpath : string, namespaces : { key : string, value : string )
 

E.g.:

Copy
var xml = '<?xml version="1.0" encoding="utf-8" ?>
<x:Orders xmlns:x="http://myuri">
        <x:Order x:id="O1">
            <Product id="1" name="Nexus 5">
                <Price>400.00</Price>
                <Qty>1</Qty>
            </Product>
            <Product id="5" name="Wireless Charger">
                <Price>50.00</Price>
                <Qty>1</Qty>
            </Product>
        </x:Order>
        <x:Order x:id="O2">
            <Product id="2" name="IPhone">
               <Price>800.00</Price>
                <Qty>1</Qty>
            </Product>
            <Product id="5" name="Wireless Charger">
                <Price>50.00</Price>
                <Qty>1</Qty>
            </Product>
        </x:Order>
</x:Orders>';
var doc = server.Xml.Load(xml);
var products = doc.Query("/x:Orders/x:Order/Product[@name='Wireless Charger']", {'x' : 'http://myuri' });
log(products[0]['id']);
log(products[1]['id'])

Node API Calls

The table below lists the properties you can access on the nodes within an XML document.

Property Returns Description
HasAttributes Boolean Indicates if an element has at least one attribute.
Property Value: true if the current node has attributes; otherwise, false.
HasElements Boolean Indicates if an element has at least one child element.
Property Value
: true if the current node has child elements; otherwise, false.
AttributeCount Number Returns the number of attributes on the current node (element).
Property Value
: The number of attributes if the current node (element) has attributes; otherwise, null.
ElementCount Number Returns the number of elements on the current node.
Property Value
: The number of elements if the current node (element) has child elements; otherwise, null.
Elements() : Element[] Array of strings Returns all child elements of a node element.
Elements(name : string)
To specify a namespace, use following syntax for name {http://myuri.org}name
Array of strings Returns all child elements with the specified node element.
this[attributeName : string ]
To specify a namespace for the attribute, use following syntax for name {http://myuri.org}name
String Returns the value of the specified node attribute.