0

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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!

1 Comment

use this only delete price,can you write a function to do this ?I use xml..[tagName] ,but flash can't compile, and each time use delete xml..price[0] isn't a good way ,i think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.