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>
4

2 に答える 2

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>
于 2016-07-06T12:19:08.540 に答える
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);

ヤフオクで見てみよう!詳細については、 devnet の記事またはRoger の E4X の記事を参照してください。

関連するスタックオーバーフローの質問:

HTH

于 2011-02-22T16:56:53.287 に答える