2012-10-25 73 views
0

例如,我想刪除所有價格標籤及其內容。刪除xml對象中的相同標籤節點

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> 

我使用刪除xml..price,沒有工作,刪除操作僅適用於第一級,我想從整個樹刪除標籤,有沒有一些簡單的方法來做到這一點?

回答

1

事實是deleting XML nodes in as3 is harder than it looks。那篇文章涵蓋了它的基礎知識。您基本上需要遍歷所有節點並delete他們逐一,使用數組語法

你的情況:

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

希望這有助於!

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

要通過名稱參數中刪除所有的節點,可以使這樣的功能:

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

+0

使用此唯一的刪除價格,你可以編寫一個函數來做到這一點嗎?我使用xml .. [tagName],但flash無法編譯,並且每次使用delete xml..price [0 ]不是一個好方法,我想。 – Max

2

您也可以使用過濾器表達式做在一個線那麼:

deleteAllTag(xml, "price"); 

活的示例le at wonderfl:http://wonderfl.net/c/cHfy

+0

這很酷! +1 –

+0

同意,這是真棒,應該是被接受的答案 – danii