Today we will learn how to load xml files into our projects. We will use xml file containing some text data. If you haven’t read the previous parts of AS3 Basics here they are: part1, part2, also if you don’t know what is XML check out wikipedia.
Reading xml in AS3 is much easier then it was in AS2 you don’t have to use this wierd ‘node’ syntax. Our xml structure looks like this:
<?xml version="1.0" encoding="utf-8"?> <data> <news date="07-04-2009"> <title> Here is title of our message. </title> <message> Here is our message. </message> </news> <news date="06-04-2009"> <title> Here is another title of our message. </title> <message> Here is our another message. </message> </news> </data>
At the top of xml file we have the header which contains xml version and encoding information, under the header we have data element (it’s our root element). Inside we have the news elements each contains one attribute called date and two elements called title and message. I think the structure is very clear and doesn’t need further explanation.
To read xml file in actionscript 3.0 we need a URLLoader, URLRequest, XML object and event listener that will listen for complete event.
package { import flash.events.Event; import flash.net.URLLoader; import flash.net.URLRequest; import flash.display.Sprite; public class Actions extends Sprite { private var xml:XML; // our xml file public function Actions(){ readXML(); } private function readXML():void { var loader:URLLoader = new URLLoader(new URLRequest("news.xml")); loader.addEventListener(Event.COMPLETE, xmlLoaded); } private function xmlLoaded(e:Event):void { xml = new XML(e.target.data); } } }
We will focus on two function’s readXML and xmlLoaded. In the first function we create the loader variable which is assigned to URLLoader then we pass the name of our xml file (in my case it’s news.xml), then we assign the Complete event listener with the xmlLoaded function as a second parameter. So when the loading of xml is completed the xmlLoaded function will be called. In xmlLoaded function the data from the loader (e.target.data) is assigned to our xml object. After this assignment we can use the xml data. Here are some examples how to read elements and attributes of our xml file.
trace(xml.news[0].title); //output = Here is title of our message.
We read the title of first news element.
trace(xml.news[0].@data); //output = 07-04-2009
We read the date attribute of our first news element.
Conclusion: If you want to read the element use ‘.’ + name if you want to read the attribute of element use ‘.@’ + name.
This is the end of our short tutorial that covered the loading of xml files. In part 4 of ActionScript 3.0 Basics (hopefully tomorrow) we will make news reader component that will load the text (containing html tags) from the xml file, and then we will insert it into a dynamic TextField.







why are you extending MovieClip? you are adding more to the filesize doing that!
hehe you are right I dont know why I did it I always extend Sprite ;P THX for letting me know!
[...] free XML Loader Class by Clemente Gomez. You can get the source code here. Recently I have done a tutorial on loading xml data, it will help you to understand this class or you can try to make your own XML [...]
trace(xml.news[0].@data);
//output = 07-04-2009
I think it should say @date ;)
Thanks for the post, I always forget how this works.
yeap you are right ;) Thank you for the corection, chears m8…
[...] 3.0 Basics part 1. ActionScript 3.0 Basics part 2. – Simple Menu, using tweener Tutorial. ActionScript 3.0 Basics part 3 -loading xml data. ActionScript 3.0 Basics part 4 – news reader [...]
Nếu mình sử dụng link từ file xml trong as3.0 thi lam sao ban!?? thanks!
[...] 3.0 Basics part 1. ActionScript 3.0 Basics part 2. – Simple Menu, using tweener Tutorial. ActionScript 3.0 Basics part 3 -loading xml data. ActionScript 3.0 Basics part 4 – news reader [...]
var xml:XML;
//———————————————————————
Show_XML.addEventListener(MouseEvent.CLICK,action);
//———————————————
function action(event:MouseEvent):void
{
readXML();
}
//function showdata(event:MouseEvent):void
//{
// action();
//
//}
function readXML():void
{
var loader:URLLoader=new URLLoader(new URLRequest(“news.xml”));
loader.addEventListener(Event.COMPLETE,xmlLoader);
}
function xmlLoader(event:Event):void
{
xml=new XML(event.target.data);
trace(xml.news[0].title1);
trace(xml.news[0].@date);
xml.news[0].@date=”08-04-2009″;
trace(xml.news[0].@date);
}
[...] ( if you are not fallowing along I recomend to read the previous tutorials: part 1, part 2 , part 3). Now it’s time to combine this knowlage in one project. We will make simple animated news [...]
[...] http://www.blog.mpcreation.pl/actionscript-30-basics-part-3-loading-xml-data/ Comments (0) [...]
I think is very good!! ~_~
data or date . I think I know it!!!