2011-02-22 3 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

あなたは簡単に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); 

は、より多くの詳細については、Yahoo! devnet articleまたはRoger's E4X articleを見てください。

関連stackoverflowの質問:

HTH

+0

ありがとうジョージ!それは私を助けてくれました!私はそれを行う簡単な方法がなければならないことを知っていた - 私は間違いなくそれらの記事を読んでいる –

0

あなたは子孫の名前を知らないか、同じ属性を持つ別の子孫を選択しますあなたが使用できる値:

destinations.descendants( "*")。要素()。(属性( "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> 
関連する問題