Battletech Live

Online, Turn-Based Battletech – Development Logs

Deleting elements from an XML-driven tree control

I’ve been struggling with a number of concepts this weekend, mainly how to add and remove objects from a tree component that uses XML as a source.  There are a number of blogs available that talk about XML, XMLLists, how AS3 uses E4X, which is good, but most don’t go into enough detail to be truly useful.  Within XML structures, you can have siblings, which are other nodes, descendants, and attributes.  As a brief example, consider the following block:

<xml label="friends">
     <element kind="user" label="Aleksizz" icon="boy0" gender="M" age="new"/>
     <element kind="user" label="Le Singe" icon="boy0" gender="M" age="old"/>
     <element kind="group" label="Oklahoma">
          <element kind="user" label="berzerker" icon="boy12" gender="M" age="old"/>
          <element kind="user" label="Granpa_jo" icon="boy28" gender="M" age="old"/>
          <element kind="user" label="zardozbtech" icon="boy4" gender="M" age="old"/>

The text block corresponds to the following tree.  In the lower-right is a trash can.  The idea is that the items within the tree structure can be moved around and if any of them need to be deleted, they can be dragged to trash.  When this happens, the item gets removed from the tree and placed in the trash, reflected by the change in the icon.  If the user wants to review what’s placed in the trash, they can click on the can and  a small panel will display those items.  Everything, up to this point, sucked.  The struggle was not to remove an item from the tree or from the XML, but in finding which item to remove.  If I do a trace on folderTree.length before anything is placed into the tree, I find that it already contains 3 items, which I couldn’t figure out how to reference.  After the friends list was built, the tree shows six viewable rows, but if I trace folderTree.length again, the result is 17.  wtf?  Looking at the XML above, there nine rows, so that doesn’t add up either.  In the end, the code that got it to work is as follows:

delete friendSource..element.(@label == String(trashGrid[trashGrid.length-1][‘label’]))[0];

In the XML listing, every displayed item begins with “element”.  In the string x.y.z, y is a child of x and z is a child of y.  But, an attribute is part of a node, not a descendant, so you might say x.y.@z.  Make no sense so far?  This might help.  The double dots after friendSource, tells the system to search for all “element” nodes within friendSource, regardless of relationship.  Inside the parenthesis, it says that if the label attribute of the found element is equal to the label value of the last item to be placed into the trash can, then we have a match.  And finally, at the very end we have [0].  I still don’t know quite what that part does, but I know that without it, it doesn’t get deleted.  [0] typically represents the first element of a sequence or an array.

The next task to be completed is dragging items from the trash back out into the tree.  That bit is typically something like this:

friendSource = friendSource.prependChild(friendRecord);

prependChild tells it to place the new record at the beginning of everything else.  appendChild would place it at the end.  There are also commands to tell it to place the record before or after a particular node.  I should have that done tomorrow and started on the buttons for adding and removing groups.

Picture 1Picture 2Picture 3


August 9, 2009 Posted by | Project Development | Leave a comment