OpenWGA 7.5 - OpenWGA Concepts and Features

Design and development » TMLScript

XML support

Processing XML can be done in two ways in TMLScript.

One way is using the object WGA.Xml and the methods found there. This utilizes a Java XML parser called Dom4J and the DOM document that is parsed from XML is represented by the Java object org.dom4j.Document. The Dom4J API is quite powerful and feature rich but does not embed natively into the TMLScript language.

Another way is using the native XML support that is embedded to TMLScript which follows a standard called ECMAScript for XML or "E4X". This standard introduces a native XML object and some new syntaxes to work with it in an intuitive and language-native way. 

The XML object can be created using the "new" statement while using an XML string as input:

var order = new XML("<order><customer country="de"><firstname>John</firstname><lastname>Doe</lastname></customer></order>");


But it is also possible to directly state the XML code as a literal. So this is equivalent:

var order = <order><customer country="de"><firstname>John</firstname><lastname>Doe</lastname></customer></order>;

Using curly braces {} you can dynamically calculate certain parts of the XML literal as expressions, for example to initialize the data from variables:

var country="de";

var firstName="John";

var lastName="Doe";


var order = <order><customer country={country}><firstname>{firstName}</firstname><lastname>{lastName}</lastname></customer></order>;

Once an XML object is created it can be queried for its DOM contents using property syntax. The XML object itself represents the root element of the document:

order.customer[0]
order.customer[0].@country

Note the property beginning with "@" which is used to query an element attribute. Note also how we use array index syntax to select the first customer element of the document.

This also works when writing to properties. The following example writes "en" into the attribute country of the first customer element:

order.customer[0].@country = 'en';

If you write to an element or attribute that does not yet exist then TMLScript will create it. This allows quite elegant initialisation of XML data:

var order = <order/>;

order.customer[0] = ""; // Creates element <customer> with empty content

order.customer[0].firstname = "John"; // Creates element <firstname>

order.customer[0].lastname = "Doe"; // Creates element <lastname>

order.customer[0].@country = "de"; // Creates attribute "country"

If you only wanted to create one <customer> tag you could also omit the index.

You could also use the XML literal syntax to add new nodes to your XML document using the "+=" operator. This adds a new customer

order.customer += <customer country="fr"><firstname>Jane</firstname><lastname>Due</lastname></customer>;

Note that we omit the index here to tell the XML object that we want to append a new element of this name.

To put out the contents of an XML object into string form again use the method "toXMLString()" available on all XML objects:

return order.toXMLString();

This is only a small part of the syntax introduced with E4X. See this dokument on E4X examplest to find out more.