Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter xml nodes by nested children values in ActionScript 3?

I have a very simple scenario where I have a structure similar to this

<tours>
    <tour>
        <name>Italy 1</name>
        <destinations>
            <destination>Rome</destination>
            <destination>Milan</destination>
        <destinations>
    </tour>
    <tour>
        <name>Italy 2</name>
        <destinations>
            <destination>Rome</destination>
            <destination>Venice</destination>
        <destinations>
    </tour>
</tours>

Now I want to query all the tours that go to Milan.

Below is the logical format that I can think of based on EX4 style

XmlData.tour.(destinations.destination.(name == "Milan"))

But of course this doesn't work.

What is the correct way of pulling this data without using any extra logic?

like image 931
Ardalan Ravanbakhsh Avatar asked Dec 17 '12 12:12

Ardalan Ravanbakhsh


2 Answers

XmlData.tour.destinations.destination is XMLList. It contains XMLs, which looks like <destination>Milan</destination> Each these XML have name = destination, so you get nothing as result.

Try this:

XmlData.tour.(destinations.destination.children().contains("Milan"));
like image 182
AtomicRobot Avatar answered Oct 29 '22 18:10

AtomicRobot


1)Badly formatted xml. (missing '/' on closing destinations)

2)You seem to have run into some kind of bug in AS3 E4X-finder. This was really weird, here's a workaround though...

var foo:XMLList = data.tour.destinations.(destination == "Milan");
trace("direct check:  " + foo); //fails - 0 matches
trace("------");
for each(var child:XML in data.tour.destinations.destination) {
    if (child == "Milan") {
        trace("found match in foreach Milan");
    }
}

Further investigating, it seems like the E4X-engine screws up since you have multiple children inside a tag with the same identifier (<destination>).

typing following makes the "filter function" behave as expected:

<destinations>
<destination2>Rome</destination2>
<destination>Milan</destination>
</destinations>

.... that's really weird... Anyone who can elaborate on this? Because according to the xml-standards of E4X it should be possible to do it as done in the question.

like image 40
Daniel MesSer Avatar answered Oct 29 '22 19:10

Daniel MesSer