How do I display XML descendants of a node with a specific attribute in AS3?
I've been trying to figure out how to display the descendants (in this case exchangeRate and PlacesOfInterest) of a parent node with a specific attribute.
To set the scene - the user clicks on a button which sets a string variable to a destination eg. japan or australia.
The code then runs through a set of nodes in the XML and any that have a matching attribute is traced - simple enough
What I can't figure out is how to then display only the child nodes of the node with that attribute.
I'm sure there has to be a way of doing it and I'll probably be banging my head against the desk when I find it, but any help would be greatly appreciated!
public function ParseDestinations(destinationInput:XML):void
{
var destAttributes:XMLList = destinationInput.adventure.destination.attributes();
for each (var destLocation:XML in destAttributes)
{
if (destLocation == destName){
trace(destLocation);
trace(destinationInput.adventure.destination.exchangeRate.text());
}
}
}
<destinations>
<adventure>
<destination location="japan">
<exchangeRate>400</exchangeRate>
<placesOfInterest>Samurai History</placesOfInterest>
</destination>
<desti开发者_如何转开发nation location="australia">
<exchangeRate>140</exchangeRate>
<placesOfInterest>Surf and BBQ</placesOfInterest>
</destination>
</adventure>
</destinations>
You should be able to easily filter nodes with E4X in as3:
var destinations:XML = <destinations>
<adventure>
<destination location="japan">
<exchangeRate>400</exchangeRate>
<placesOfInterest>Samurai History</placesOfInterest>
</destination>
<destination location="australia">
<exchangeRate>140</exchangeRate>
<placesOfInterest>Surf and BBQ</placesOfInterest>
</destination>
</adventure>
</destinations>;
//filter by attribute name
var filteredByLocation:XMLList = destinations.adventure.destination.(@location == "japan");
trace(filteredByLocation);
//filter by node value
var filteredByExchangeRate:XMLList = destinations.adventure.destination.(exchangeRate < 200);
trace(filteredByExchangeRate);
Have a look at the Yahoo! devnet article or Roger's E4X article for more details.
Related stackoverflow questions:
- Select XML nodes by attribute in AS3
- a question about parsing xml file in Flex
HTH
If you don't know the name of descendant or you want to select different descendants with same attribute value you can use:
destinations.descendants("*").elements().(attribute("location") == "japan");
For example:
var xmlData:XML =
<xml>
<firstTag>
<firstSubTag>
<firstSubSubTag significance="important">data_1</firstSubSubTag>
<secondSubSubTag>data_2</secondSubSubTag>
</firstSubTag>
<secondSubTag>
<thirdSubSubTag>data_3</thirdSubSubTag>
<fourthSubSubTag significance="important">data_4</fourthSubSubTag>
</secondSubTag>
</firstTag>
</xml>
trace(xmlData.descendants("*").elements().(attribute("significance") == "important"));
Result:
//<firstSubSubTag significance="important">data_1</firstSubSubTag>
//<fourthSubSubTag significance="important">data_4</fourthSubSubTag>
精彩评论