AS3 Delete child node from XML by child value

十年热恋 提交于 2020-02-03 05:27:37

问题


I've got an XML with a structure like the following;

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

How do I delete the item with the value 458. Just to clarify this, I don't know the index of that item, so simply calling delete items[index] won't do here. I have to delete by value.

Any hints?


回答1:


Using e4x filtering and the possibilities of using function inside the filter you can delete the node you want :

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

so combining these infos 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());



回答2:


this should solve it. Btw this will delete all direct chilren with name "item" that have value 458.

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

To delete recursively all children and subchildren that have name "item" and value 458 use:

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


来源:https://stackoverflow.com/questions/9682641/as3-delete-child-node-from-xml-by-child-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!