15. Parsing and creating XML in Haxe

2014-09-16

Parsing and creating XML in Haxe

Haxe has a built-in library for parsing and working with XML data.

There are two provided APIs for working with XML in Haxe, one that works similarly to other DOM (Document Object Model) parsers, and another, which is closer to the E4X format.

Both are easy to use, and choosing one over another is only a matter of preference. In this article I will show you how to use the DOM-based XML parser. For debugging purposes, you can use the trace() function to display the string representation of an XML object in the log window.

Let's start by making an XML object out of an already existing XML String. We will use the static parse() method of the Xml class:

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

As a result we get an Xml-typed object, which we can modify using the built-in methods.

Let's try creating the same structured XML from scratch using the provided methods:

var xml:Xml = Xml.createElement("root");

var item1:Xml = Xml.createElement("item");

item1.addChild(Xml.createCData("Hello"));

xml.addChild(item1);

var item2:Xml = Xml.createElement("item");

item2.addChild(Xml.createPCData("World"));

xml.addChild(item2);

As you can see, we use the createElement() method whenever we want to create a node. We then create 2 "item" nodes using the same method, and add those to the root node. The content of the "item" children in this case is added using 2 different methods - createCData() and createPCData(). Both represent text, however, one is surrounded in CData tags, which means that all data inside of the text node is stored as-is.

The resulting XML looks like this:

<root><item><![CDATA[Hello]]></item><item>World</item></root>

We can add comment elements too:

var comment:Xml = Xml.createComment("This is a comment");

xml.addChild(comment);

We can add attributes and their values using the set() method:

var xml:Xml = Xml.createElement("root");

xml.set("hello", "world");

Which would result in:

<root hello="world"/>

XML children can be easily looped through. There are several iterators available. For example, we can simply loop through all children of a node:

for (child in xml) {

	trace(child);

}

The log window will print all the children:

<item><![CDATA[Hello]]></item>

<item>World</item>

<!--This is a comment-->

We can loop through elements only using the elements() method:

for (child in xml.elements()) {

	trace(child);

}

The results are all elements. You can see that the comment block, for example, is not included:

<item><![CDATA[Hello]]></item>

<item>World</item>

It is possible to filter nodes by their name:

for (child in xml.elementsNamed("item")) {

	trace(child);

}

The results are all "item" nodes:

<item><![CDATA[Hello]]></item>

<item>World</item>

Looping through attributes is also possible:

for (child in xml.attributes()) {

	trace(child + " = " + xml.get(child));

}

That code would trace attributes and their values like this:

hello = world

As I said, this is only the first XML API out of 2 that are available in Haxe. The second API is called Fast, and is explained in the next tutorial.