Adobe Flex – Filter XML Data using E4X and RegExp
Posted on : 09-05-2009 | By : Michael Fitchett | In : ActionScript, Development, Flex
2
Here is an example
Here is the code for the example
<!-- ===================================================================================== Get data using HTTPService from fitchett.me RSS Feed ===================================================================================== --> <!-- ===================================================================================== Script ===================================================================================== --> <![CDATA[ private function init():void { //Once all of the components have loaded send the HTTPService request myData.send(); } private function dataLoaded():void { //Once the data has loaded populate the DataGrid and TextArea with the Results. myDataGrid.dataProvider = myData.lastResult.channel.item; } private function search():void { try { var reg:RegExp = new RegExp(tbSearch.text.toLowerCase()); myDataGrid.dataProvider = myData.lastResult.channel.item.(reg.test(title.toLowerCase())) } catch(err:Error) { //Do Nothing... } finally { //Do Nothing... } } ]]> <!-- ===================================================================================== Display Components ===================================================================================== --> |
You can download the project source here Flex E4X Example (716)
For those of you wondering how this example was done without reading the code read on…
Getting the Data
- Flex gets the XML data from the RSS feed using the HTTPService
- Once the data has successfully loaded the data is then passed into the DataGrid
Filtering the Data using E4X and RegExp
- When a user types into the textbox a change action is triggered triggering the search() function
- The search() function then runs code that creates a new RegExp variable named “reg”with the “tbsearch.text.toLowerCase()”being passed in as the string.
- The DataGrid provider is then … umm iono man it’s just magic to me at this point.
- And poof everything works!
Ok maybe I do know.
5. Since we are using E4X we parse the XML using the new RegExp we just created.
As always ping me if you have questions with a comment or feel free to e-mail me michael@fitchett.me











MICHAEL!
Awesome stuff. You totally rock! Thank you very much for posting this.
As many experienced Flex developers have pointed out, using E4X to work with XML data in Flex is supposed to be the most efficient approach. However, very few people have shown me any actual examples of getting all of this to work.
So, I’m especially grateful to you for a) taking the time to put together an example and b) being generous enough to share it with the rest of the world.
Finally, I always kid around with other developers and tell them that [insert your favorite programming language] is all bout knowing the syntax. After that, it’s programming as usual. So when I went back to my original code, I seriously had to make two syntax changes, and everything started working. I wonder, are we Flex developers like golfers? Coming back to it, time after time, to get a little better each time and figure out new techniques to keep things interesting?
At any rate, A BIG THANK YOU for all your work and help.
I really appreciate your contribution, I took me a while to figure out how to compare strings with E4X using a regular expression and not only the == comparator.
Thanks a lot.