Recently, I was working on my first Flex project where I was consuming XML via a service. Whenever I wanted to make data changes with the service, I was required to send in an XML packet. Since AS3 has native support for XML literals, I wanted to create my small XML packet inline. For example:
var changeSet:XML = ACTIVEQUOTE ;
With my XML content defined as a literal, all I needed was to dynamically set some attribute values from a value object. Using curly braces { }, I can bind to certain values. For example:
var policy:PolicyVO = note.getBody() as PolicyVO; var changeSet:XML = ACTIVEQUOTE ;
Originally, I wrapped the curly braces with double quotes from my experience with MXML, but that does not apply here. With the double quotes, AS3 thinks you want the literal string rather than evaluating what is within the curly braces. Simply stripping the double quotes worked. A neat thing about this is that in AS3 you can also dynamically name tags too. For example, I could do this:
var rootTag:String = "Users"; var changeSet:XML = Joe Smith ;
For a great resource for everything E4X in ActionScript 3, check out the AS3 E4X Rundown!
Paul Johnson Said:
Thanks Javier. Bookmarked for future reference…