2011-02-22 38 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> 
相關問題