Skip to main content
shiko_89
Participant
January 6, 2015
Question

Reading from an xml and adding data to a datagrid AS3

  • January 6, 2015
  • 3 replies
  • 423 views

I am trying to read from an xml file which contains flight info and want to display them in a datagrid or a table in AS3, but I can't manage to do that. Can someone point me to the right direction. Thank you in advance.

This topic has been closed for replies.

3 replies

shiko_89
shiko_89Author
Participant
January 11, 2015

Thank you for your answer. I've done an example but I have a problem with it the dataProvider only reads 2 tags in the xml file and adds it in the DataGrid. here is the xml file.

<?xml version="1.0" encoding="ISO-8859-1"?>

<airlines>

    <flightdescription>

        <id> 1 </id>

        <flightno> usa1000 </flightno>

        <origin>Chicago</origin>

        <destination>Boston</destination>

        <departurdatetime>2015/03/29 14:45 </departurdatetime>

        <arrivaldatetime>2015/03/30 23:45</arrivaldatetime>

        <img src='C:\Users\sherif\Desktop\Fujitsu - Digital media solution\Flash\US-Airways-Logo'/>

    </flightdescription>

</airlines>

I've shortened the xml file.

and this is the as3 code:

var myDataGrid:DataGrid = new DataGrid();

myDataGrid1.addColumn("flight no");

myDataGrid1.addColumn("origin");

myDataGrid1.addColumn("destination");

myDataGrid1.addColumn("departure date & time");

myDataGrid1.addColumn("arrival date & time");

  var dp:DataProvider;

     

var urlAirlines:String = "airlines.xml";

var reqAirlines:URLRequest = new URLRequest(urlAirlines);

var urlLoader:URLLoader = new URLLoader();

urlLoader.addEventListener(Event.COMPLETE, AirlinesHandler);

urlLoader.load(reqAirlines);

function AirlinesHandler(event:Event):void {

    var xmlDP:XML = new XML(urlLoader.data);

    dp = new DataProvider(xmlDP);

    myDataGrid1.dataProvider = dp;

    //myDataGrid1.columnCount = mydataGrid1.width;

    myDataGrid1.rowCount = myDataGrid1.length+2;

    myDataGrid1.columns[0].width = 70;

    myDataGrid1.columns[1].width = 70;

    myDataGrid1.columns[2].width = 90;

    myDataGrid1.columns[3].width = 130;

    myDataGrid1.columns[4].width = 130;

    myDataGrid1.horizontalScrollPolicy = ScrollPolicy.ON;

 

    trace(dp);

    trace(myDataGrid1);

}

the output only reads the tags Origin and Destination and adds them in DataGrid. anyone have any ideas?? Thank you in advance.

kglad
Community Expert
Community Expert
January 11, 2015

your column names should match the xml tag names (even down to the typos).

shiko_89
shiko_89Author
Participant
January 14, 2015

Thank you very much for your reply. I have one more question there is an image tag in my xml can I add it to my dataGrid??

kglad
Community Expert
Community Expert
January 6, 2015

use the urlloader class to load your xml file.

if that's a cross-domain xml file and you can't add a policy file to the xml hosting server, using a php gateway is a good work-around.

robdillon
Participating Frequently
January 6, 2015