如何在 AS3 中显示具有特定属性的节点的 XML 后代?
我一直在试图弄清楚如何显示具有特定属性的父节点的后代(在本例中为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够在 as3 中使用 E4X 轻松过滤节点:
查看 雅虎! devnet 文章 或 Roger 的 E4X 文章 了解更多详细信息。
相关stackoverflow问题:
华泰
You should be able to easily filter nodes with E4X in as3:
Have a look at the Yahoo! devnet article or Roger's E4X article for more details.
Related stackoverflow questions:
HTH
如果您不知道后代的名称或者您想选择具有相同属性值的不同后代,您可以使用:
destinations.descendants("*").elements().(attribute("location") == "japan" );
例如:
结果:
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:
Result: