You can load the data from the remote xml file using the HTTPService in Flex. To read the data from the xml file do not forget to set the resultFormat property of the HTTPService as e4x.
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
verticalAlign="middle" backgroundColor="#FFFFFF">
<mx:Script>
<![CDATA[
import com.riacoder.examples.vo.EmployeeVO;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
[Bindable]
private var empCollection:ArrayCollection;
public var xmlURL:String = "http://localhost:8080/flex_xml/employees.xml";
private function loadXml():void
{
httpService.url = xmlURL;
httpService.send();
}
private function onFault(event:FaultEvent):void
{
var title:String = event.type + " ("+ event.fault.faultCode +")";
var text:String = event.fault.faultString;
Alert.show(text, title);
}
private function onResult(event:ResultEvent):void
{
var xmlList:XMLList = XML(event.result).children();
empCollection = new ArrayCollection();
for each(var obj:Object in xmlList)
{
var empVO:EmployeeVO = new EmployeeVO();
empVO.fName = obj.fname;
empVO.lName = obj.lname;
empVO.uName = obj.uname;
empVO.gender = obj.gender;
empVO.age = obj.age;
empVO.email = obj.email;
empVO.image = obj.image;
empCollection.addItem(empVO);
}
}
]]>
</mx:Script>
<mx:HTTPService id="httpService"
showBusyCursor="true"
resultFormat="e4x"
fault="onFault(event)"
result="onResult(event)" />
<mx:ApplicationControlBar dock="true">
<mx:Button label="Load Employees" click="loadXml()" />
</mx:ApplicationControlBar>
<mx:TileList id="employeeList" dataProvider="{empCollection}"
itemRenderer="com.riacoder.examples.components.EmployeeRenderer" columnCount="2" rowCount="3"
columnWidth="350" rowHeight="160" color="#323232"/>
</mx:Application>
I have created a ItemRenderer to display the employee details for this example. I have named the file as EmployeeRenderer.mxml. See the below code.
EmployeeRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" width="350" height="180">
<mx:Image source="{data.image}" width="100" height="100" verticalAlign="top" />
<mx:VBox>
<mx:Label text="First Name: {data.fName}" />
<mx:Label text="Last Name: {data.lName}" />
<mx:Label text="User Name: {data.uName}" />
<mx:Label text="Gender: {data.gender}" />
<mx:Label text="Age: {data.age}" />
<mx:Label text="E-Mail Id: {data.email}" />
</mx:VBox>
</mx:HBox>
I have created the ValueObject class named as EmployeeVO to keep a employee data.
EmployeeVO.as
package com.riacoder.examples.vo
{
[Bindable]
public class EmployeeVO
{
public var fName:String;
public var lName:String;
public var uName:String;
public var gender:String;
public var age:String;
public var email:String;
public var image:String;
public function EmployeeVO()
{
}
}
}
Content of the xml which I have used in this example
employees.xml
<?xml version="1.0" encoding="utf-8"?> <employees> <employee> <fname>Senthil Kumar</fname> <lname>Ranganathan</lname> <uname>senthil</uname> <gender>Male</gender> <age>26</age> <email>senthil.friends@gmail.com</email> <image>http://localhost:8080/flex_xml/images/4.png</image> </employee> <employee> <fname>Ponbharathi</fname> <lname>Bakthaduruvan</lname> <uname>pon</uname> <gender>Male</gender> <age>25</age> <email>kbponbharathi@gmail.com</email> <image>http://localhost:8080/flex_xml/images/5.png</image> </employee> <employee> <fname>Vinoth</fname> <lname>Kumar</lname> <uname>vinoth</uname> <gender>Male</gender> <age>27</age> <email>vino.mib@gmail.com</email> <image>http://localhost:8080/flex_xml/images/3.png</image> </employee> <employee> <fname>Anto</fname> <lname>Robin</lname> <uname>anto</uname> <gender>Male</gender> <age>29</age> <email>anto@xyz.com</email> <image>http://localhost:8080/flex_xml/images/5.png</image> </employee> </employees>