Thursday, September 04, 2008

Easy loading Xml with as3.0

This project will show you how to load data from Xml file.
Actionscript 3.0 help you work with xml more easier than actionscript 2.0 you will love it.
First create xml file like this



We will load this file store value of attributes "name" and value of "url" feilds.
Start Codding!
- Create new as file name loadXml.as and add this code.
package
{
import flash.xml.*;
import flash.events.*;
import flash.net.*;
public class loadXml
{
public var ImageUrlArr:Array;
public var ImageNameArr:Array;
function loadXml(path:String)
{
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest(path));
}
function LoadXML(e:Event):void
{
var xmlData:XML = new XML ();
xmlData.ignoreWhite = true;
xmlData = new XML (e.target.data);

for (var k in xmlData.pic.url.attribute("name"))
{
ImageNameArr[k]=xmlData.pic.url.attribute("name")[k];
}
for (var i in xmlData.pic.url.text())
{
ImageUrlArr[i]=xmlData.pic.url.text()[i];
}

}
}
}
On main time line add tis code.

var lxml:loadXml = new loadXml ("your xml path");

If you want to use the data you can access this variable "lxml.ImageNameArr" and
lxml.ImageUrlArr.
You can implement this example to a flash image gallery or anything else.
easy job.


Hudsadin keox

No comments: