Tuesday, February 17, 2009

Delete a node from XML / XMLListCollection

source

PROBLEM SUMMARY

simply using the Delete key work in Flex does not work most of the time and if you have a Tree that's bound to the XMLListCollection Flex will mess up the Tree Selection after deleting an XML element.
SOLUTION SUMMARY

Use a custom method to for loop and delete proper child and reset tree selection
EXPLANATION
__________________________________________________________________________________
Use this method:
private function xmlDeleteNode(xmlToDelete:XML):Boolean
{
var cn:XMLList = XMLList(xmlToDelete.parent()).children();

for ( var i:Number = 0 ; i < cn.length() ; i++ )
{
if ( cn[i] == xmlToDelete )
{
delete cn[i];
return true;
}
}

return false;

}

Also, remember to do on an XML Bound Tree Control:
myTree.selectedItem = null;
_______________________________________________________________________________

i have extended this logic
myTree.selectedItem = null;
after delete ... instead setting the selectedItem to null, it wud b gud
if you select the deleted node's parent
before calling the xmlDeleteNode method

save the xmlToDelete's parent
**************************************
var tmp:XML = xmlToDelete.parent();
xmlDeleteNode(xmlToDelete);
myTree.selectedItem = tmp

**************************************

No comments:

Freelance