RichPurple - new wings to your business

XML calling in AS3

Wednesday 20th August, 2008

Author: Kamal
Share this news on emailEmail Print this newsPrint

AS3.0 has amazingly improved and is now based on the java syntaxing. Calling XML in AS 3 reduces your work effort up to 80% and gives you freedom to get data using the xml node (tag) name rather then using node index.

The simplest way to understand this=> earlier we used to write this code in AS2 to get the large xml file:

loader.lText.text = 'Reading Data....';

dataXML = new XML();

dataXML.ignoreWhite = false;

dataXML.onLoad = loadData;

url = global_path+"xml/_team.xml";

dataXML.load(url);

var member:Array = new Array();

var _client:Array = new Array();

var credit:Array = new Array();

var _technology:Array = new Array();

function loadData () {

if (this.loaded && this.hasChildNodes) {

xml = this.childNodes;

for (i=0; i<xml.length; i++){

if (xml[i].nodeName == "parent") {

var xmlValues = xml[i].childNodes;

credit.push(xml[i].attributes.name);

credit.push(xml[i].attributes.client);

credit.push(xml[i].attributes.credit);

credit.push(xml[i].attributes.description);

for (i2=0; i2<xmlValues.length; i2++){

if (xmlValues[i2].nodeName == "team") {

var temp:Array = new Array();

temp.push(xmlValues[i2].attributes.id);

temp.push(xmlValues[i2].attributes.username);

temp.push(xmlValues[i2].attributes.password);

temp.push(xmlValues[i2].attributes.name);

temp.push(xmlValues[i2].attributes.post);

temp.push(xmlValues[i2].attributes.phone);

temp.push(xmlValues[i2].attributes.email);

temp.push(xmlValues[i2].attributes.cl);

temp.push(xmlValues[i2].attributes.pl);

temp.push(xmlValues[i2].attributes.login);

temp.push(xmlValues[i2].attributes.loggedin);

temp.push(xmlValues[i2].attributes.im1);

temp.push(xmlValues[i2].attributes.im2);

temp.push(xmlValues[i2].attributes.url);

member.push(temp);

}

if (xmlValues[i2].nodeName == "client") {

var temp_client:Array = new Array();

temp_client.push(xmlValues[i2].attributes.id);

temp_client.push(xmlValues[i2].attributes.name);

temp_client.push(xmlValues[i2].attributes.org);

temp_client.push(xmlValues[i2].attributes.add);

temp_client.push(xmlValues[i2].attributes.post);

temp_client.push(xmlValues[i2].attributes.phone);

temp_client.push(xmlValues[i2].attributes.email);

temp_client.push(xmlValues[i2].attributes.im1);

temp_client.push(xmlValues[i2].attributes.im2);

temp_client.push(xmlValues[i2].attributes.url);

temp_client.push(xmlValues[i2].attributes.skype);

_client.push(temp_client);

}

if (xmlValues[i2].nodeName == "technology") {

var a1:String = xmlValues[i2].attributes.name;

_technology.push(a1.split(', '));

}

}

}

}

//trace(member);

play();

}

}

 

Don't kill me for this long code.

Happy time: now we write this small code to get the entire data at once:

var xmlloader:URLLoader = new URLLoader();

var xrequest:URLRequest = new URLRequest(this.global_path+"xml/_team.xml");

this.xmlloader.load(this.xrequest);

var configuration:XML;

this.xmlloader.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent){trace("Error: " +e)});

this.xmlloader.addEventListener(Event.COMPLETE, this.getPath);

function getPath(event:Event):void  {

this.configuration = new XML(this.xmlloader.data);

//trace('Configuration done');

// Write your code here!;

}

Further on the same, once the fetching code is done, now next you need to initialize the data. and bingo! your effort is reduced more then 70%.

In AS 2.0 you need to define chains of array to get large data and then hit and trial to get it in the correct form and at correct pace. AS 3.0 gives you the freedom to get the data using the xml tagname/nodename directly. so if your xml is this:

<data>

<myData>data 1</myData>

<myData>data 2</myData>

<myData>data 3</myData>

</data>

You can get the data using nodename "configuratio.data.myData[aray-index]".

It's more than anything you want to get XML in flash. AS 3.0 Rocks.

BACK TO NEWSDESK
XML calling in AS3

Add a Comment

*

*

comments

CAPTCHA *

* Mandatory fields.

horizintal bar