Tuesday, July 24, 2007

ActionScript 3, E4X Snippet

I have been working on an E4X function to transform XML attributes into elements for displaying in a Tree.  I have a working function, not exactly in the form I wanted, but I am lacking a good example of it to post this week.  This is all tied into the work I have posted in the last few weeks about sorting XML for trees.

Code snippet

There was one little code snippet which was part of solving the problem I thought I would share with you now.  Unlike my XML sort routines where I modify the source object, this routine creates a duplicate XML object but with elements instead of attributes.

The problem I had was coming up with code to copy the root element of the source code to a new XML object.  It wasn't so straight forward with E4X.  I think concise code could have been written to simply copy the object with the copy() function and the delete its children but I was concerned this could be inefficient especially if the object was large.

This is what I did:

 1 public static function transformXmlAttributeToElement
 2     ( avXmlIn               :XML,
 3       avAttributeName       :String,
 4       avArrayAttributeNames :Array )
 5     :XML
 6 {
 7     var lvXmlOut:XML = <{avXmlIn.name()}/>;
 8     for each( var lvXmlAttribute:XML
 9                in avXmlIn.attributes() )
10        lvXmlOut.@[lvXmlAttribute.name()] = lvXmlAttribute;
    ...

Line 7 creates a root element with the same name as the source.  Lines 8 and 9 loop through all of the attributes in the source root element with line 10 creating a duplicate of each attribute in the destination element.

My generic functions can't use hard coded names, but most examples you find on the Internet use hard coded names.  I thought maybe someone needing to do something similar could benefit from the example.  I prefer to write more comprehensive posts than this but I'm just not ready this week.

Addendum

My post More Hierarchical Sorting E4X XML: for Flex Tree & Beyond was my 1st attempt to make use of publishing a Flex application along with the Publish Application Source feature of Flex Builder 2.  Boy am I dumb.  For some reason it got into my head that every time I recompile, the new source code would be regenerated.  Not true.  If you downloaded the source code before last Friday, you got old source code.  I've fixed that.

I also fixed a state transition flicker problem that crept back into the example.  There appears to be a little bit of art in handling flicker in state changes.

2 comments:

CresceNet said...

Gostei muito desse post e seu blog é muito interessante, vou passar por aqui sempre =) Depois dá uma passada lá no meu site, que é sobre o CresceNet, espero que goste. O endereço dele é http://www.provedorcrescenet.com . Um abraço.

Notebooks said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the Notebook, I hope you enjoy. The address is http://notebooks-brasil.blogspot.com. A hug.