In the previous tutorial on using XML with Flex 2.0, I covered how to create an object of class XML and use that object to get data out of the XML document. In this tutorial I'll go over how to use the data pulled out of an XML object in a Flex application.
Our first step is to get the XML data. There are many different ways to get XML data into a Flex 2.0 application. You could use ActionScript 3.0 classes HttpService or WebService. You could also use MXML tags mx:HttpService or mx:WebService to do the same tasks. In the Flex 2.0 Developer's Guide, it describes how to use these features in the material on RPC Components.
What I've done in this tutorial is use ColdFusion to get the XML data and then provide that XML data to my Flex application. I placed the code that gets the XML data inside a CFC function. I then use Flex 2.0's built in ability to call CFC functions to call the function that returns the XML data. In CFMX 7 CFCs can return type XML.
If you visit this tutorial's demo, you can right click on the Flex 2.0 application to the view the source code and my comments. The application works by having the user type in a musical artist or band's name. Then the user clicks on the Get XML button. The button's click event causes the Flex 2.0 application to call the CFC function to get the XML data.
The CFC function uses the artist name provided to connect to a REST application on MusicBrainz. This application will retun an XML file that provides all the releases by the artist (or band). Once the CFC gets the data from MusicBrainz, it creates an XML object and returns it to the Flex application.
When the result is returned by the CFC, the handleResult function is executed. Flex treats the data returned by the CFC as an Object. We can use this Object to create our XML class object. I've bound the text area in my Flex application to display this XML object created in the handleResult function.
Normally, once you have the XML object created you can use the dot notation we discussed in tutorial 1 to get data out of the XML object. However, I was not able to use the dot notation with the XML object created using the data returned by MusicBrainz. Class XML does have an elements method that I was able to use to pull release titles out of the XML object. If anyone knows why the dot notation did not work for this XML object, please post a comment.
As I pulled out each release's title, I added the title to an ArrayCollection. This ArrayCollection is the data provider for a datagrid that will display all the titles.
If your XML file is relatively simple, you can use the XML object as the argument to create an ArrayCollection object. In this case the XML data returned by MusicBrainz was too complex, so I first stripped out all the release titles and just put the titles in the ArrayCollection.
In our next tutorial, we will explore using class HTTPService to get the XML data without going through ColdFusion.