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?
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"));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With