E4X in Flex

I've just started working on an Adobe Flex project, and I've messing around with E4X. I'm familiar with Xpath 1.0 from working with Konfabulator widgets, but it took me a while to get to grips with E4X. Most of what I learned, I got from this website. So, I'll just go ahead and use some of the material from the site. Here's what I've learned so far. I'll be using the below XML as an example.

<?xml version="1.0"?>
<rdf>
<channel>
<title><![CDATA[Test file]]></title>
<link>http://www.sephiroth.it</link>
<description><![CDATA[A custom description of this document]]></description>
<language>en-us</language>
<items>
<item id="001">
<resource url="http://www.sephiroth.it/tutorials.php" />
<channel>FLASH-PHP</channel>
<author email="alex@alex.com">
<name>Alessandro</name>
</author>
</item>
<item id="002">
<resource url="http://www.sephiroth.it/tutorials/flashPHP/E4X" />
<channel>Flash 9</channel>
<author email="mich@alex.com">
<name>Michael</name>
</author>
</item>
<item id="003">
<resource url="http://www.sephiroth.it/tutorials/flashPHP/scrollRect" />
<channel>Flash 8</channel>
<author email="fk@alex.com">
<name>Frank</name>
</author>
</item>
</items>
</channel>
</rdf>


httpResult.lastResult..item will give all the items in XML fromat:


<item id="001">
<resource url="http://www.sephiroth.it/tutorials.php" />
<channel>FLASH-PHP</channel>
<author email="alex@alex.com">
<name>Alessandro</name>
</author>
</item>
<item id="002">
<resource url="http://www.sephiroth.it/tutorials/flashPHP/E4X" />
<channel>Flash 9</channel>
<author email="mich@alex.com">
<name>Michael</name>
</author>
</item>
<item id="003">
<resource url="http://www.sephiroth.it/tutorials/flashPHP/scrollRect" />
<channel>Flash 8</channel>
<author email="fk@alex.com">
<name>Frank</name>
</author>
</item>

httpResult.lastResult..name.text() will give all names in normal text, without the XML formatting. That's what the .text() is for.

AlessandroMichaelFrank


httpResult.lastResult..item.@id will give the ids of every item:

001002003

httpResult.lastResult..item[1].@id will give the id of the second item. But there's a warning that popped up in the debugger that discourages from using square bracket operator. It recommends using the ArrayCollection.getItemAt() instead.


002

After knowing all that, I can virtually get any value I want. But there's still along way to go in mastering Flex.

2 comments:

ZenMaster said...
This comment has been removed by the author.
ZenMaster said...

E4X in Flex, sounds cool. But the code you have put up is a little hard to read.