One thing, among others, that Flex doesn’t have, and is important because many of its components needs XML structures, is deleting (taking out) nodes from a XML structure. This is crucial for a tree or for a menu where you need to change it dynamically. So bellow is a recursive method that will go through the whole XML and will remove the nodes specified in an array.
In goThrough method nodes are searched by the id_data XML attribute but with some minor changes you can make it search for node name.
public var menubarXML:XML = <root> <menuitem label="new" id_data="top"> <menuitem label="file" id_data="file"/> <menuitem label="document" id_data="document"/> <menuitem label="folder" id_data="folder" enabled="false" /> <menuitem label="other" id_data="other"/> </menuitem> <menuitem label="edit" id_data="top"> <menuitem label="undo" id_data="undo"/> <menuitem label="redo" id_data="redo"/> <menuitem type="separator"/> <menuitem label="cut" id_data="cut"/> <menuitem label="copy" id_data="copy"/> <menuitem label="paste" id_data="paste"/> <menuitem type="separator"/> <menuitem label="delete" id_data="delete"/> <menuitem label="refresh" id_data="refresh"/> </menuitem> <menuitem label="help" id_data="top"> <menuitem label="help" id_data="help"/> <menuitem label="search" id_data="search"/> <menuitem label="about" id_data="about"/> </menuitem> <menuitem label="logoff" id_data="top"> <menuitem label="logoff" id_data="logoff"/> </menuitem> </root>; public function goThrough(xml:XMLList, removeData:Array):void { if (xml.length() > 0) { for (var i:int = xml.length() - 1; i >= 0 ; i--) { if (removeData.indexOf(xml[i].@id_data.toString()) != -1) { delete xml[i]; } else { if (xml[i].children()) { goThrough(xml[i].children(),removeData); } } } } }
The method is called like:
goThrough(menubarXML.children(), new Array('document','delete'));
and by accessing menubarXML afterwards you’ll have the XML with the specified nodes — having “document” and “delete” in id_data attribute — removed. This is working very smoothly because Flex uses, mainly, the Singleton pattern and most complex data is accessed by memory reference and this enables us that after modifying menubarXML we will have its structure modified although this was done “locally” inside a method. The debugText is a TextArea for output and in this case the taken out nodes are displayed.
So this is it and I think will help many of you a lot. We are waiting for comments and you may improve it to suit your needs.
Tags: ActionScript, structure, xml
This post was written by Andrei Ionescu
Views: 8250











He great stuff, here something simular but with an arraycollection.
http://labs.flexcoders.nl/2007/03/29/flex-treecontrol-search-sample/
AS3 does have a very poorly documented “delete” function that you can use to delete a node and all its children. You can see an example I posted on Kirupa.
Thank you Aaron!
Hey guys, may I know how to create a XMLList while Im using to get dynamic data? I need to refresh my data grid which get data dynamically from a php page.
Good work, thanks! But fails while deleting following nodes. Needs to decrement index (i--) after deleting the node.
Cengiz, thanks for the comment. You’re right if you tried to delete to consecutive nodes in the same parent it won’t work.
Instead of decreasing the “i” variable, as you proposed, I’m looping from the last node to the first.
Now it should work.
Thank you, this is great.
Thanks for posting this. I just found one error when trying to use it… You need to access the nodes in xml, not xml.children initially. You will check the child nodes during recursion (the way it was before, every other level of the tree was being skipped completely). I have posted the corrected version below.
Kara, thank you for your comment. You’re right. I implemented the changes in the article.