Latest Entries »

XML Data into Data Grid in AS3

Step 1 : drag DataGrid Component into stage and delete it.

Step1. Paste below code

import fl.data.DataProvider;
import fl.controls.DataGrid;
import fl.controls.dataGridClasses.DataGridColumn;

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

xmlLoader.load(new URLRequest(“sampleXML.xml”));

function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
ParseBooks(xmlData);
}

function ParseBooks(bookInput:XML):void {
trace(“XML Output”);
trace(“————————“);

var titleList:XMLList=bookInput.Book.title;
var authorList:XMLList=bookInput.Book.author;

var dp:DataProvider=new DataProvider(xmlData);

var dg:DataGrid = new DataGrid();
dg.setSize(400, 300);
dg.editable=false;

var col:DataGridColumn;
col = dg.addColumn(“Author”);
col.dataField =”author”;
col = dg.addColumn(“Title”);
col.dataField =”title”;
dg.dataProvider=dp;

addChild(dg);
}

3. Make xml file (sampleXML.xml)

<?xml ver=”1.0″ ?>
<Books>
<Book ISBN=”0553212419″>
<title id=”1″>Sherlock Holmes :Complete Novels …</title>
<author>Sir Arthur Conan Doyle</author>
<date>2010/10/12</date>
</Book>
<Book ISBN=”0743273567″>
<title id=”1″>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
<date>2010/10/14</date>
</Book>
<Book ISBN=”0684826976″>
<title id=”1″>Undaunted Courage</title>
<author>Stephen E. Ambrose</author>
<date>2010/10/20</date>
</Book>
<Book ISBN=”0743203178″>
<title id=”3″>Nothing Like It In the World</title>
<author>Sir Arthur Conan Doyle</author>
<date>2010/10/30</date>
</Book>
</Books>

4.Run Program


Here, I have made some code stuff which could create object looping and can access each object’s property from outside of loop

// importing reated files
import flash.display.Sprite;
import flash.text.TextField;

// Varial declearation
var answer:Array=new Array();
var ansGroup:Array=new Array();
var options:Sprite=new Sprite();

answer[0]=[“nepal”,”china”,”bhutan”,”usa”];
answer[1]=[“Lichi”,”mango”,”banana”,”apple”];
answer[2]=[“sunday”,”monday”,”tuesday”,”saturday”];
answer[3]=[“cat”,”lion”,”monkey”,”donkey”];

for(var i:Number=0; i<4; i++){
// making object
var myTextField:TextField=new TextField();

// Setting property of created object
myTextField.x=i*130;
myTextField.y=25;
myTextField.width=120;
myTextField.height=25;
myTextField.text=answer[0][i];
myTextField.border=true;

// put into ansGroup Array
ansGroup.push(myTextField);

// put into displaycontainer options
options.addChild(myTextField);

}
// Setting displaycontainer property of x and y position
options.y=100;
options.x=25;

// displaying displayContainer
addChild(options);