AS3 Remove node child from XML by child value

I have XML with a structure similar to the following:

<items> <item>5</item> <item>3006</item> <item>25</item> <item>458</item> <item>15</item> <item>78</item> </items> 

How to delete an element with a value of 458. To clarify this, I do not know the index of this element, so just calling the delete [index] elements will not be here. I have to delete by value.

Any clues?

+4
source share
2 answers

Using e4x filtering and the ability to use the function inside the filter, you can remove the node you want:

  • xml.item. (text () == value) will give you the node you are looking for
  • valueOf () will give you the current node you are filtering
  • delete will delete node

combining this information you can do:

 var xml:XML=<items> <item>5</item> <item>3006</item> <item>25</item> <item>458</item> <item>15</item> <item>78</item> </items>; function deleteValue(xml:XML, value:String):void{ xml.item.((text()==value) && (delete parent().children()[valueOf().childIndex()])); } deleteValue(xml, "458"); trace(xml.toXMLString()); 
+4
source

it should decide. Btw this will remove all chilren lines with the name "item" that have a value of 458.

 delete xml.(item == "458"); 

To recursively delete all child and child elements with the name "item" and a value of 458, use:

 delete xml..(item == "458"); 
+7
source

Source: https://habr.com/ru/post/1401143/


All Articles