Max
Max

Reputation: 8045

delete same tag node in an xml object

for example i want to delete all price tag and their contents.

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>

i use delete xml..price ,didn't work, the delete operation only works on the first level, i want to delete the tags from the whole tree, is there some easy way to do it?

Upvotes: 0

Views: 71

Answers (2)

Patrick
Patrick

Reputation: 15717

You can also do it in one line using filter expression:

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

To remove all node by a name parameter you can make a function like this:

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

and then:

deleteAllTag(xml, "price");

Live example at wonderfl : http://wonderfl.net/c/cHfy

Upvotes: 2

danii
danii

Reputation: 5693

The fact is that deleting XML nodes in as3 is harder than it looks. That article covers the basics of it quite well. You basically need to loop through all nodes and delete them one by one, using array syntax.

In your case:

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

And the ouput is:

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

Hope this helps!

Upvotes: 1

Related Questions