2011-02-22 82 views
0

我一直在想出如何显示具有特定属性的父节点的后代(在本例中为exchangeRate和PlacesOfInterest)。如何在AS3中显示具有特定属性的节点的XML后代?

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

的代码,然后通过在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> 

回答

0

你应该能够轻松地与E4X在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); 

看一看在Yahoo! devnet articleRoger's E4X article了解更多详情。

相关计算器问题:

HTH

+0

三江源乔治!这帮了我很多!我知道必须有一个简单的方法 - 现在我肯定会阅读这些帖子 – 2011-02-22 17:20:44

0

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

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> 
相关问题