0

たとえば、すべての値札とその内容を削除したいとします。

var xml:XML = <breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
light Belgian waffles covered with an assortment of fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>

delete xml..price を使用しましたが、機能しませんでした。削除操作は最初のレベルでのみ機能します。ツリー全体からタグを削除したいのですが、簡単な方法はありますか?

4

2 に答える 2

2

フィルタ式を使用して、1行で実行することもできます。

xml..price.( delete parent().children()[valueOf().childIndex()] );

名前パラメータですべてのノードを削除するには、次のような関数を作成できます。

function deleteAllTag(xml:XML, tag:String):void{
 xml.descendants(tag).(delete parent().children()[valueOf().childIndex()] );
}

その後:

deleteAllTag(xml, "price");

wonderflでの実例: http://wonderfl.net/c/cHfy

于 2012-10-25T09:45:16.963 に答える
1

実際のところ、as3 で XML ノードを削除することは、見た目よりも困難です。その記事は、その基本を非常によくカバーしています。基本的に、配列構文deleteを使用して、すべてのノードとそれらを 1 つずつループする必要があります。

あなたの場合:

//to select all price nodes:
trace( "—- xml..price —-" );
trace( xml..price );

trace( "—- delete in loop —-" );

//loop
for each (var price:XML in xml..price)
{
  //and delete each node!
  delete xml..price[0];
}

trace( "—- after delete —-" );
trace(xml);

出力は次のとおりです。

—- xml..price —-
<price>$5.95</price>
<price>$7.95</price>
<price>$8.95</price>
<price>$4.50</price>
<price>$6.95</price>

—- delete in loop —-

—- after delete —-

<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <description>light Belgian waffles covered with strawberries and whipped cream</description>
    <calories>900</calories>
  </food>
  <food>
    <name>Berry-Berry Belgian Waffles</name>
    <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
    <calories>900</calories>
  </food>
  <food>
    <name>French Toast</name>
    <description>thick slices made from our homemade sourdough bread</description>
    <calories>600</calories>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
    <calories>950</calories>
  </food>
</breakfast_menu>

お役に立てれば!

于 2012-10-25T09:23:31.327 に答える