删除xml对象中的相同标记节点

时间:2012-10-25 08:51:26

标签: actionscript-3 flash

例如,我想删除所有价格标签及其内容。

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,没有工作,删除操作仅适用于第一级,我想从整个树中删除标签,有一些简单的方法吗?

2 个答案:

答案 0 :(得分:2)

您也可以使用过滤器表达式在一行中执行此操作:

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

答案 1 :(得分: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>

希望这有帮助!