Adobe Flex – Filter XML data using E4X
Posted on : 31-03-2009 | By : Michael Fitchett | In : Flex, Quick Tips
2
You have imported your data using either an HTTPService, Web Service or some other means. Great! now how do you filter and sort the data? Using E4X of course! Lets get started.
1st. Switch the resultFormat on your HTTPService or Web Service by adding the following property resultFormat=”e4x” to your tag:
<!-- HTTPService --> <mx:HTTPService id='data' resultFormat="e4x"/> <!-- Web Service --> <mx:WebService id="wsData" wsdl="http://www.domain.com/Service.asmx?WSDL" showBusyCursor="true"> <mx:operation name="getData" resultFormat="e4x">
2nd. Now that we are returning the XML data in E4x format we can now use E4x to filter the data. Here is a quick example of how you would filter the data in a DataGrid:
<!-- HTTPService -->
<mx:DataGrid dataProvider="{data.lastResult.root.item(somenode != null)}"/>
<!-- Return One Record/Row/Result-->
<mx:DataGrid dataProvider="{data.lastResult.root.item(somenode != null)[0]}"/>
<!-- Web Service -->
<mx:DataGrid dataProvider="{wsData. getData.lastResult.root.item(somenode != null)}"/>
<!-- Return One Record/Row/Result-->
<mx:DataGrid dataProvider="{wsData. getData.lastResult.root.item(somenode != null)[0]}"/>
This is just one way of doing it, you have tons of different options. It all really depends on your project; typically, I bind the data to an XMLListCollection then filter the data by changing the source with the filter code and running refresh on the XMLListCollection.











Michael, I just need a simple E4X datagrid example.
Can you help us out?
All we want to do is
1) use HTTPService to get data remotely
2) with resultFormat = “e4x”
and then
3) populate a two-or-three-column datagrid with the XML data
4) without arrayCollection
5) straight from XML
For the last two days, I have been struggling with the syntax. Everything works fine until I start using E4X. The XML displays properly in another application, where I’m using the default resultFormat (object).
Since Adobe and other FLEX gurus claim that using E4X is the best approach to dealing with XML files — but nobody provides a simple example — I was hoping that somebody here could tell me how it’s done. From what I’ve been able to find out, there are many other Flex developers who have a hard time with that as well.
Thanks for any ideas or links, etc.
I just went live with a post that should help you http://www.fitchett.me/?p=177