[SOLVED]How do we use xml and Xpath in openfl?

Hello everyone,
I have been trying to code a program that just retrieves an xml file over the internet and reads it and prints the info I want.
In order to do so, I am using wamp and daniel cassidy’s haxe-xpath that I got from github.

This small project was supposed to just take 5 min but it has been more than three days :flushed:. I don’t know what I did wrong and where.

Here is my simple xml file:

<?xml version="1.0" encoding="utf-8"?>
<a>
     <b>
          <c>eh ben voilà</c>
     </b>
</c>

Here is my class main:

class Main extends Sprite{

    var xml:Xml;
    var xmlLoader:URLLoader = new URLLoader();
    var xpathXml:XPathHxHxml;

    function new(){
        super();
        xmlLoader.addEventListener(Event.COMPLETE,onComplete);
        xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
        xmlLoader.load(new URLRequest("http://localhost/example.xml");
    }

    function onError(err:IOErrorEvent){trace("the error is: "+err);}

    function onComplete(e:Event){

        xml = Xml.Parse(xmlLoader.data);
        xmlLoader.removeEventListener(Event.COMPLETE,onComplete);
        xmlLoader.removeEventListener(IOErrorEvent.onError);

        xpathXml = XPathHxXml.warpNode(xml);

        var xpathExpression = new Xpath("/a/b/c");
        var result = xpathExpression.evaluate(xpathXml);
        trace(result);
    }
}

I was hoping to have result from Trace saying “eh ben Voilà!”

but what I got was different:

{ booleanValue => false, nodes => { iterator => #function:0 }, typeName => node set, numberValue => 0, stringValue => }

now my question is, did I forget something?
or if the traced result is the correct one, how do I retrieve data from my xml using xpath

I found the answer myself… I just really need to take some time and learn xpath itself.
I needed to change the var result = xpathExpression.evaluate(xpathXml); into var result = xpathExpression.selectNodes(xpathXml);

and put a for loop.

for( i in result){
trace(i.getStringValue());
}

//output:

eh ben voilà

Is there any reason you need to use XPath? Haxe has a great class called Fast that is built for parsing Xml. You would write something like:

xml = Xml.parse(xmlLoader.data); var fast = new Fast(xml.firstElement());

To access the field you would have:

var result : String = fast.node.b.node.c.innerData;

I haven’t tested the code but it is just an example. You can find out more about Fast here

1 Like

Yes I know about Fast, but I don’t know how to select multiple nodes without really knowing where they are located.
if I have a very long but well structured xml file, and I just need to retrieve all c data element from all the b node.

in a case like those, I don’t know if Fast will be fast enough.

while with Xpath i just have to write in xpathExpression="//b/c";

So I heard of Xpath and it seems to be what I need.

You can get all c nodes under b in Fast with:

fast.node.b.nodes.c

This returns an iterable list of nodes that can be accessed with the same syntax. For example:

for ( c in fast.node.b.nodes.c ) {
    trace( c.innerData );
}

or

for ( b in fast.nodes.b ) {
    for ( c in b.nodes.c ) {
         trace( c.innerData );
    }
}

Is this what you were looking for?

1 Like