mkz2zg
Last Updated: February 25, 2016
·
914
· ooflorent

Haxe and XML CDATA

Haxe does not properly read XML CDATA.

Considering the following XML:

<root>
    <tag>
        <![CDATA[foo]]>
    </tag>
</root>

And the below Haxe snippet:

// `data` contains the above XML.
// Parses `data`.
var xml = haxe.xml.Fast(Xml.parse(data).firstElement());

We are unable to read the CDATA value of the tag:

// Reads the inner data of `tag`
var content = xml.node.tag.innerData;

// > Exception is thrown!
// > 'tag does not only have data'

A simple fix is to trim the spaces before and after the CDATA element, then to parse the XML string:

// Normalize CDATA
data = ~/>\s+<!/gs.replace(data, "><!");
data = ~/]>\s+</gs.replace(data, "]><");