问题
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