如何在 AS3 中显示具有特定属性的节点的 XML 后代?

发布于 2024-10-18 18:00:12 字数 1234 浏览 5 评论 0原文

我一直在试图弄清楚如何显示具有特定属性的父节点的后代(在本例中为 ExchangeRate 和 PlacesOfInterest)。

要设置场景 - 用户单击一个按钮,将字符串变量设置为目的地,例如。日本或澳大利亚。

然后,代码运行 XML 中的一组节点,并跟踪任何具有匹配属性的节点 - 足够简单

,我不明白的是如何仅显示具有该属性的节点的子节点。

我确信必须有一种方法可以做到这一点,当我找到它时,我可能会用头撞桌子,但任何帮助将不胜感激!

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜声 2024-10-25 18:00:12

您应该能够在 as3 中使用 E4X 轻松过滤节点:

 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);

查看 雅虎! devnet 文章Roger 的 E4X 文章 了解更多详细信息。

相关stackoverflow问题:

华泰

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:

HTH

妖妓 2024-10-25 18:00:12

如果您不知道后代的名称或者您想选择具有相同属性值的不同后代,您可以使用:

destinations.descendants("*").elements().(attribute("location") == "japan" );

例如:

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"));

结果:

//<firstSubSubTag significance="important">data_1</firstSubSubTag>
//<fourthSubSubTag significance="important">data_4</fourthSubSubTag>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文