XML Basics with Flex 3
If you’re still using arrays and text files to load content, it’s time to upgrade.
With XML, you can condense a whole mess of seemingly unrelated information into a well organized and easy to read file. Today we’ll be looking at how to take advantage of this with Flex 3. This tutorial will teach how to load an external XML file into Flex and then filter the results into labels.
Set Up the Workspace
Open up the Flex 3 Builder and start a new MXML application. I have named mine xmlSandbox for this example. Set the layout to vertical and press finish.

Before we can load an XML file we’ll need to have one. I’ve made one for this tutorial which you can download. Make a new folder within your ‘src’ folder by right clicking the icon and selecting New>Folder. Name this new folder “assets” and import the downloaded XML file into it. Once you’ve done that, we’re ready to link to it in the Flex document.
Load the XML
MXML makes loading external files easy with the HTTPService tag. Write the following code immediately under the opening application tag.
<mx:HTTPService url="assets/content.xml" id="siteData" resultFormat="e4x"/>
You just created an HTTP Service with an id of ‘siteData’ which links to the XML file in the assets folder. The results are formatted in e4x, which allows us to filter through the contents of the XML file. In order for this url to be loaded, we must tell the application to send the request. We do this by adding the event ‘creationComplete’ to the opening application tag.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="siteData.send()">
The creationComplete event is called when the page loads. In the above code we are saying then when the page is finished loading, the HTTPService named ‘siteData’ should be sent. Take a moment to test your application. It should run without errors if the XML is being loaded correctly.
Filter XML Data
The XML data will need a place to store itself while we work with it. Make a script tag immediately below the opening application tag and declare the following variable.
<mx:Script> Â Â Â Â Â Â <![CDATA[ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â private var siteContent:XMLList; ]]> </mx:Script>
The XMLList type allows us to load XML data in and work with it. To demonstrate this we’re going to build a function that will load data from the XML file and display it in labels. Add the following function to your script tag.
private function showPage(targetPage:String):void{
            //Search for title of target page
            siteContent = siteData.lastResult.page.(@location==targetPage).title;
            //Show result in label
            headerText.text=siteContent;
            //Search for body text of target page
            siteContent = siteData.lastResult.page.(@location==targetPage).text;
            //Show result in label
            bodyText.text=siteContent;
         }Now below the HTTPService tag we’re going to insert some components for the results to load into. We are also adding buttons to control navigation.
<mx:VBox>
      <mx:Label fontSize="24" id="headerText" text="Click a Button"/>
      <mx:Label id="bodyText" text="The choice is yours!"/>
      <mx:HBox>
         <mx:Button label="Home Page" click="showPage('home')"/>
         <mx:Button label="About Page" click="showPage('about')"/>
         <mx:Button label="Contact Page" click="showPage('contact')"/>
      </mx:HBox>
   </mx:VBox>So what exactly did we just do? Here’s an overview of what is happening:
- When one of the buttons is pressed it passes a string to the showPage function.
- showPage receives this parameter as a string called targetPage.
- This targetPage will determine which XML node is to be displayed.
- Each node of the XML file has an attribute called “location” which corresponds to the passed value of targetPage.
- The siteContent XMLList matches a node to the targetPage and then loads the correct title and body text into their respective labels.
Conclusion
There you have it! Run your application. If everything is working as it should, the data from the XML file is loaded up for each page when a button is pressed. Not bad! You aren’t limited to just text. Try loading the url of an image from an XML file with Flex. XML has a wide number of possibilities to explore.















Discussion
January 9th, 2009 at 12:41 AM
ThanQ for giving such guidence………cheers !
February 3rd, 2009 at 6:48 PM
Zach,
New to Flex downloaded the source compiled ok but buttons do not change the content. Have checked the file structure etc…
Any ideas ?
February 3rd, 2009 at 7:15 PM
Zach,
Problem resolved ! Was not running Webserver. Thanks for the great posting !
February 3rd, 2009 at 7:20 PM
Avron,
Sorry to hear you’re having problems! Let’s get you straightened out! Have you tried uploading your compiled files to a server?
From experience, I can tell you that Flex and Flash have very specific rules with HTTP requests on files locally. Generally if you try to open a page that requests external files through HTTP requests on your local machine, it will not work. It’s a nuisance, and I know that the Flex Builder can compensate for it. I cannot speak for the SDK, but I would assume the same
This would be my guess at the problem. Upload the related files to a server and see if they load properly. Let me know if that fixes anything for you.
February 3rd, 2009 at 8:32 PM
Hi Zach, thanks you were right of course. Seems as if a “file” is not a URL to flex.
One further interest, I would like to load html, can you describe a way in which I may do that ?
I have changed the label to
and in the function showPage(), have … bodyText.htmlText=siteContent;
it seems as if each new (xml complient) tag is output on a newline
-
-
New Home Page
-
-
Here is a whole lot of text that should go in here and then I am going to put in a
bold
tag
-
About Page
More about this site.
-
Contact Us
Get in touch!
outputs :
Here is a whole lot of text that should go in here and then I am going to put in a
bold
tag
Since I am really hacking here, any help will be appreciated ;-)
February 3rd, 2009 at 10:29 PM
@Avron
I’m planning on writing up a tutorial on htmlText and styling sometime in the near future. In the meantime I recommend you use a combination of XML to load the html, and then refer to this documentation Adobe has on parsing it correctly: http://livedocs.adobe.com/flex/3/html/help.html?content=textcontrols_04.html
Good Luck!
March 10th, 2009 at 6:38 PM
Thanks for a tutorial/example that actually works and explains what is going on. I’ve tried a bunch of tutorials from the Adobe site that won’t compile. This was just what I needed. Thanks again and keep up the good work!
March 26th, 2009 at 5:37 AM
Thanks for this mate, exactly what I was looking for!
April 6th, 2009 at 5:57 AM
This is very nice but if you write the code for the image show from xml.
it is more best.
April 29th, 2009 at 10:38 PM
Gracias por el tutorial , es lo que estaba buscando
July 15th, 2009 at 8:21 AM
Hi, i am new to flex and have taken a few classes on it but still i’m unable to read two(2) xml files into the external script and hence, the data is not displayed while running the mxml application. can anyone sort what the problem is, though i have used the same procedure for reading a single xml file into mxml?
November 19th, 2009 at 12:59 AM
HI, Please visit to read news
November 19th, 2009 at 1:00 AM
Thanks It’s Greate Tutorial.
November 22nd, 2009 at 4:07 AM
thnx 4 the tutorial and discussion…
i faced the problem of running the app on local machine bt now i kno what to do… :)
January 14th, 2010 at 6:56 PM
@Zach Dunn
Thanks ;)
@Avron
find
bodyText.text=siteContent;
change
bodyText.htmlText=siteContent;
open xml data and change text tag
<![CDATA[
Adobe Flex,test,font, orhtml
]]>
demo: http://cnr.gen.tr/flex/
January 14th, 2010 at 6:58 PM
text tag
<![CDATA[
Adobe Flex,test,font, orhtml
]]>
Join the Conversation!
Remember: Life's not all doom and gloom, so please keep it constructive. If we've made an error or missed something big, please let us know! Learning is revisions, after all.