Flex‎ > ‎

Flex and XML datasource

You can create an data source using XML.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="data.send()">
<mx:HTTPService id="data" url="document.xml"/>
<mx:DataGrid x="56" y="250" width="950" dataProvider="{data.lastResult.root.node}"/>

Explanation :

  1. We define the data source with HTTPService
    1. we define the url relative to the location of the SWF file
    2. We load it with the event of "creationComplete" on the application level.
  2. We also show the data in a grid here (this is of course optional).
    1. The DataGrid refers to the nodes under root. This is suitable for XMLs of the following sort :
We can substitute the reference to root/nodes with a popuplate to ArrayCollection

import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
private var dataCollection:ArrayCollection;
private function resultHandler(evt:ResultEvent):void

<mx:HTTPService id="data" url="document.xml" result="dataHandler(event)"/>
<mx:DataGrid x="56" y ="250" width="950" dataProvider="{dataCollection}"/>

In this code we are sending the result of the HTTPService to a function.
In the fu nction, we populate an ArrayCollection with the data we want to display.
We then display this data in a DataGrid (like before) only this time it is easier - we simply reference the collection we populated.

Nicer Error Handling

If there's an error openning the XML you might get a nasty Error popup.
You can replace that with a customized error message like this.

import mx.controls.*;
private function faultHandler(evt:FaultEvent):void{
    Alert.show("Error Title","Error content : " + evt.message);



<mx:HTTPService id="data" url="document.xml" fault="faultHandler(event)"


If the XML does not load properly - you might get an error popup or simply an empty table.
You should install the flex debugger to your browser - now if there is an error you will see the popup.

If the permissions are incorrect you should verify that the "use-network" flag is marked as false.
Check out this wiki page that has more info on the subject.