How do I display XML descendants of a node with a specific attribute in AS3?

耗尽温柔 提交于 2019-12-11 17:55:09

问题


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>   
        <destination location="australia">
            <exchangeRate>140</exchangeRate>
            <placesOfInterest>Surf and BBQ</placesOfInterest>
        </destination>
    </adventure>
</destinations>

回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/5081111/how-do-i-display-xml-descendants-of-a-node-with-a-specific-attribute-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!