[Flashcoders] XML find and delete nodes
Posted this on Flash_tiger without any luck. I know about using delete in E4X XML to delete unwanted XML nodes, but how would you hunt down through and delete any nodes (of a specific type, say module nodes in the example below) that don't have any children? For example, if myXML happens to look like: xml modules module title=apple/ module title=orange node/ module module title=banana node/ module module title=plum/ module title=pear/ module title=pineapple node/ module /modules /xml then with that structure, I would want my code to remove the module nodes that have the title attribute values of apple, plum, and pear since those have no child nodes. I know I could figure it out through enough trial and error, but I've spent enough time on it already. Thought someone here could give a quick answer. Thanks! Jason Merrill Bank of America Global Learning Shared Services Solutions Development Monthly meetings on the Adobe Flash platform for rich media experiences - join the Bank of America Flash Platform Community ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] XML find and delete nodes
This might get you pointed in the right direction. You can get an XMLList of all the empty nodes by this kind of statement xml.modules.module.(children().length() == 0); but if you then try a simple delete on that delete xml.modules.module.(children().length() == 0); you get: Delete operator is not supported with operand of type XMLList. I'm not sure right now why that wouldn't work. But you could simply loop through the XMLList generated and do a delete on each node var emptyModule:XMLList = xml.modules.module.(children().length() == 0); var index:int = emptyModule.length() - 1; for(var i = index; i - 1; i--){ delete emptyModule[i]; } trace(xml); hth, Rob PS so as far as the delete operator I'm not sure why delete(xml.modules.module) works while something like delete(xm.modules.module.(@title == apple)) gives an error like above. I would think that both those querries result in an XMLList, in the first case the delete occurs but not in the second... maybe some lunch will help my brain work better. On 9-Jun-09, at 11:02 AM, Merrill, Jason wrote: Posted this on Flash_tiger without any luck. I know about using delete in E4X XML to delete unwanted XML nodes, but how would you hunt down through and delete any nodes (of a specific type, say module nodes in the example below) that don't have any children? For example, if myXML happens to look like: xml modules module title=apple/ module title=orange node/ module module title=banana node/ module module title=plum/ module title=pear/ module title=pineapple node/ module /modules /xml then with that structure, I would want my code to remove the module nodes that have the title attribute values of apple, plum, and pear since those have no child nodes. I know I could figure it out through enough trial and error, but I've spent enough time on it already. Thought someone here could give a quick answer. Thanks! Jason Merrill Bank of America Global Learning Shared Services Solutions Development Monthly meetings on the Adobe Flash platform for rich media experiences - join the Bank of America Flash Platform Community ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] XML find and delete nodes
Filter the XML using E4X and then make a new XML from that filtered result. var xml:XML = xml modules module title=apple/ module title=orange node/ /module module title=banana node/ /module module title=plum/ module title=pear/ module title=pineapple node/ /module /modules /xml; // var list:XMLList = xml..module.(children().length() == 0); var newXML:XML = xml/; xml.setChildren(list); trace(xml); ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] XML find and delete nodes
Actually, you don't need that newXML var. Sorry. I left that in during debugging. Just setChildren the original xml, as I did. If you want to make a copy without destroying the original, then setChildren() the newXML var instead. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] XML find and delete nodes
Since Jason wanted to get rid of all the nodes that were empty he would want to filter against !=0 and then use setChildren to refresh his original xml. Using setChildren is a nifty trick, much nicer than that delete loop I put out there. Rob On 9-Jun-09, at 12:24 PM, Steven Sacks wrote: Filter the XML using E4X and then make a new XML from that filtered result. var xml:XML = xml modules module title=apple/ module title=orange node/ /module module title=banana node/ /module module title=plum/ module title=pear/ module title=pineapple node/ /module /modules /xml; // var list:XMLList = xml..module.(children().length() == 0); var newXML:XML = xml/; xml.setChildren(list); trace(xml); ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] XML find and delete nodes
Thanks Steve - have it working. Yep, I love E4X too - still learning all the intricacies like this, so this is another notch in my belt. (however for anyone listening and for the archives, your code does the opposite of what I want. It removes all nodes WITH children. I want to remove any nodes that DON'T have children. Changing that one line to look for children length greater than 0 does the trick: var list:XMLList = xml..module.(children().length() 0); ) Thanks for your help, Jason Merrill Bank of America Global Learning Shared Services Solutions Development Monthly meetings on the Adobe Flash platform for rich media experiences - join the Bank of America Flash Platform Community -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Steven Sacks Sent: Tuesday, June 09, 2009 12:25 PM To: Flash Coders List Subject: Re: [Flashcoders] XML find and delete nodes Filter the XML using E4X and then make a new XML from that filtered result. var xml:XML = xml modules module title=apple/ module title=orange node/ /module module title=banana node/ /module module title=plum/ module title=pear/ module title=pineapple node/ /module /modules /xml; // var list:XMLList = xml..module.(children().length() == 0); var newXML:XML = xml/; xml.setChildren(list); trace(xml); ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] XML find and delete nodes
Rob, I came across the same problem when trying out various E4X approaches with delete. Steve's idea works (with the caveat his removes populated nodes instead of unpopulated ones, but I fixed that). Thanks! Jason Merrill Bank of America Global Learning Shared Services Solutions Development Monthly meetings on the Adobe Flash platform for rich media experiences - join the Bank of America Flash Platform Community -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Rob Romanek Sent: Tuesday, June 09, 2009 12:20 PM To: Flash Coders List Subject: Re: [Flashcoders] XML find and delete nodes This might get you pointed in the right direction. You can get an XMLList of all the empty nodes by this kind of statement xml.modules.module.(children().length() == 0); but if you then try a simple delete on that delete xml.modules.module.(children().length() == 0); you get: Delete operator is not supported with operand of type XMLList. I'm not sure right now why that wouldn't work. But you could simply loop through the XMLList generated and do a delete on each node var emptyModule:XMLList = xml.modules.module.(children().length() == 0); var index:int = emptyModule.length() - 1; for(var i = index; i - 1; i--){ delete emptyModule[i]; } trace(xml); hth, Rob PS so as far as the delete operator I'm not sure why delete(xml.modules.module) works while something like delete(xm.modules.module.(@title == apple)) gives an error like above. I would think that both those querries result in an XMLList, in the first case the delete occurs but not in the second... maybe some lunch will help my brain work better. On 9-Jun-09, at 11:02 AM, Merrill, Jason wrote: Posted this on Flash_tiger without any luck. I know about using delete in E4X XML to delete unwanted XML nodes, but how would you hunt down through and delete any nodes (of a specific type, say module nodes in the example below) that don't have any children? For example, if myXML happens to look like: xml modules module title=apple/ module title=orange node/ module module title=banana node/ module module title=plum/ module title=pear/ module title=pineapple node/ module /modules /xml then with that structure, I would want my code to remove the module nodes that have the title attribute values of apple, plum, and pear since those have no child nodes. I know I could figure it out through enough trial and error, but I've spent enough time on it already. Thought someone here could give a quick answer. Thanks! Jason Merrill Bank of America Global Learning Shared Services Solutions Development Monthly meetings on the Adobe Flash platform for rich media experiences - join the Bank of America Flash Platform Community ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] XML find and delete nodes
Once you really dig into E4X and realize the crazy stuff you can do, it's amazing. I do some pretty crazy E4X parsing, filtering and validation with Gaia. Here are some examples: // get all nodes named page or asset in the entire XML var nodes:XMLList = xml.descendants().(name() == page || name() == asset); // from those nodes, find any that don't have both id and src attributes var invalidNodes:XMLList = nodes.(!attribute(id).length() || ! attribute(src).length()); // get all nodes where id is not alphanumeric or begins with a number invalidNodes = nodes.(!(/^[a-z_][a-z0-9_]*$/i.test(@id))); // this one is complex but basically it isolates nodes that do not have a valid // class package path in a package attribute var packageNodes:XMLList = xml.descendants(). (attribute(package).length() 0); invalidNodes = packageNodes.(!(/^[a-z][\w\.]*\w+$/ i.test(attribute(package; As you can see, combining E4X and RegEx you can do some CRAZY stuff. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders