Parsing and creating XML in Haxe: the Fast way

posted on Sep 17, 2014 in Haxe, Syntax
Haxe Fast XML

In the previous tutorial we've looked at parsing and creating XML objects in Haxe using the built-in API which has a DOM-like access model. This time, we're looking at the second API that Haxe offers for dealing with XML data.

The API we're using is called Fast (haxe.xml.Fast package) and allows for a simpler access to XML elements using dot notation. This may save development time, because the syntax is closer to the E4X standard.

To begin, let's create a simple XML String and parse it into a regular XML object using the parse() method.

Here's the XML we're going to parse:

<root myAttr = 'test'>
	<catalog>
		<item>Hello</item>
		<item>World</item>
	</catalog>
</root>

Looks like this in the code:

var xml:Xml = Xml.parse("<root myAttr='test'><catalog><item>Hello</item><item>World</item></catalog></root>");

To use the Fast approach, we need to use the Fast class as a wrapper for the existing XML object:

var fast:Fast = new Fast(xml.firstElement());

We can now access the XML data using dot notation. For example, we can read the attribute of the root tag like this:

trace(fast.att.myAttr);

This will trace the value of the "myAttr" attribute of the root tag, which is "test".

We can also test whether an element has an attribute with a certain name. For example, we can test whether the root tag has the attribute "otherAttr", and print the value if it exists.

if (fast.has.otherAttr) {
	trace(fast.att.otherAttr);
}else {
	trace("otherAttr does not exist");
}

We can access child nodes using their name like this:

var catalog:Fast = fast.node.catalog;

Very simple. The resulting catalog variable is also a Fast instance.

We can use the name property to get the name of the node:

trace(catalog.name);

We can loop through all nodes of the same name using the "nodes" property and the name of the nodes we're looking for:

for (item in catalog.nodes.item) {
	trace(item.innerData);
}

The innerData property displays the inner data as text, such as "Hello" and "World".

We can use the innerHTML property of a Fast object to display the inner XML structure as a String without the parent node:

trace(catalog.innerHTML);

This will trace the following String:

<item>Hello</item><item>World</item>

You can retrieve an XML object out of a Fast object using the "x" property. Tracing that value will trace the same thing as innerHTML, plus the parent node:

trace(catalog.x);

Trace results:

<catalog><item>Hello</item><item>World</item></catalog>

You can loop through all children elements of a Fast object using the "elements" property. The resulting objects are all Fast instances.

for (elem in catalog.elements) {
	trace(elem.x);
}

The traced values:

<item>Hello</item>
<item>World</item>

That's all there is to the Fast XML API in Haxe.

If you found this helpful, consider taking a look at my other tutorials, as well as subscribing to the free newsletter!

14898