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

您也可以使用过滤器表达式在一行中完成:

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一一遍历所有节点和它们。

在你的情况下:

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