AS3 E4X Rundown

Monday, November 26th, 2007

AS3 E4X Rundown is a great introduction and reference to working with E4X in Flex.  The author, Roger Braunstien is correct that 75% of E4X makes perfect sense, but it’s the remaining 25% that requires a bit of demystifying.  This post covers the basics well then goes on to share some of the tips and tricks in working with E4X.

E4X Selection by Attribute in Flex

Sunday, November 18th, 2007

My colleague David Meeuwis showed me a most useful and non-intuitive tip that explains a subtle difference between using the @ axis and the attribute() axis in E4X. For example, given the following XML fragment…


<names>
<name salutation="Mr">Billy Bob Thorton</name>
<name salutation="Mrs">Angelina Jollie</name>
<name>Brad Pitt</name>
<name salutation="Ms">Jennifer Aniston</name>
<name salutation="Mr">Vince Vaugn</name>
</names>

The AS3 expression below…

myXml.name.(@salutation == 'Mr')

…will find all the names that have a salutation attribute equal to ‘Mr’. However, if there is any name in the xml that doesn’t have a salutation attribute, that query will throw an exception and die. However…

myXml.name.(attribute('salutation') == 'Mr')

Will give back all names that have a salutation attribute equal to ‘Mr’ and ignore any name elements without a salutation attribute.

So unless you are absolutely positive that the attribute you are selecting always exists, you should be using the attribute() axis over the @ axis.

Here is a quick Flex example with view source enabled that you can test with.

AJAX and Scripting Web services with E4X

Tuesday, July 11th, 2006

IBM Developer Works has an introduction to ECMAScript for XML (E4X). EX4 is an extension to JavaScript that makes XML scripting very simple. This two part series demonstrates how combining AJAX and some new XML extensions to JavaScript can DOM scripting very simple.