How to load external XML in Flash?

If you want to load external XML file in flash, first you will need to import certain flash Classes.

Let’s see which kind of flash classes we need:

– URLLoader Class — With this class we can download data from url as Text or binary data or URL-encoded variables. Now in this tutorial we will load our XML file with this Class.

– URLRequest Class — This class is also important in this process because this class will initiate our URLLoader Class to start load file.

– Event Class — Event class will notify us when the URLLoader is finished with loading or if we have the error in loading process.

– XML Class — XML class we dont need to import because this is a TOP Level class such as Array,Number,Boolean,Object.

TIP: See more detail in flash help about this Classes for better understanding!!!

Ok Let’s start with the coding:

//import necessary classes
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;

var _xml:XML;
var xmlLoader:URLLoader = new URLLoader();
var xmlURL:URLRequest = new URLRequest(“sources/xml/datas.xml”);

xmlLoader.load(xmlURL);
//now I will add event listener to URLLoader Class, first parameter in addEventListener function indicate event type, second parameter refer on function which will handle our “complete” event
xmlLoader.addEventListener(Event.COMPLETE, _completeXML);

//event handler will be called when URLLoader finished loading process.
function _completeXML(event:Event):void {

//now I will forward data from URLLoader class to XML Class
//event.target is class what is dispatch “complete” event, in our example that is URLLoader Class
_xml = new XML(event.target.data);
_xml.ignoreWhitespace = true; //igore white space in xml
_xml.ignoreComments = true; //ignore comments in xml
trace(_xml);
}

That is that external XML file is now loaded in flash and we can now start to get data from XML.

You can download source file here with previous tutorial(How to create Document Class).
SOURCE DOWNLOAD: LoadXML_AS3.zip

TIP: See more detail in flash help about this Classes for better understanding!!!

Advertisements

How create Document Class?

In a few steps I will show you how to create Document Class(MainClass) for your flash project.

First off I will create new flash document with a name _root.fla .
documentclass1

Now I will create my package folders in which I will add my document class named Application.as .

It is very important that the class must be extended like a MovieClip. When we extend our class like MovieClip that means that our Application.as Class is now sub class of the MovieClip Class and this means that the class inherited all properties and functions from MovieClip Class.

Application.as code:

package com.myname {

import flash.display.MovieClip;

public class Application extends MovieClip {

public function Application() {
trace(“Root of our Application”);

}
}
}

Now we need insert path of Application.as Class inside of document properties in Document Class Field:com.myname.Application

Now when you test your _root.fla you will see in output this message : Root of our Application
That is that now you are ready to build your application next.

Welcome to my blog!

On this blog you can find contents which are related exclusively for production of the Flash web site.

From today I will slowly begin complete my blog in order to help others who want learn flash(ActionScript). I will mostly focus on making tutorials for ActionScript 3.0.

So if you have time and will for reading and looking tutorials, add this link in your bookmarks and  go back sometimes.

Regards.

Mirko Fisic
http://www.i-do2.com