flex - xml nodes will not delete despite calling "delete" -
i'm trying use delete keyword remove nodes xml file , plain won't work.
here's stripped down example of i'm working with. every node has child named "deleteme". if value equal 1 want remove xml file. if else want leave be. delete method deffinately gettig call it's having no effect.
<?xml version="1.0" encoding="utf-8"?> <stuff> <i> <deleteme> 0 </deleteme> </i> <i> <deleteme> 1 </deleteme> </i> <i> <deleteme> 0 </deleteme> </i> </stuff>
actionscript
var xmllist:xmllist=_sourcexml.i; (var j:int=0; j < _xmllist.length(); j++) { if (xmllist[j].deleteme== 1) { delete xmllist[j]; } } //breakpoint here. xmllist still contains nodes should have been deleted xmllistcoll=new xmllistcollection(xmllist); xmllistcoll.refresh()
edit
if trace xmllist lenght before , after delete loop length indeed different. seems reason xmllist being passed xmllistcollection 1 node not deleted. makes no sense me.
edit 2
the following gives desired result. still know why former method not working.
for (var j:int=0; j < xmllist.length(); j++) { //trace(xmllist[j].deleteme) if (xmllist[j].deleteme!=1 ) { //delete xmllist[j]; xmllistcoll.additem(xmllist[j]) } }
yo
you need 'for loop' in reverse. goes removing items in collection/list via iteration loop.
when item deleted (e.g. index 2 when j 2), next item in list fills space left deleted item (e.g. index 3 becmoes index 2), j gets increased 3, , shifted item (in index 2) gets skipped. following work:
var _xmllist:xmllist=_sourcexml.i; (var j:int=_xmllist.length() - 1; j >= 0 ; j--) { if (_xmllist[j].deleteme == 1) { delete _xmllist[j]; } }
Comments
Post a Comment