In this tutorial I will show you how to read XML file and it’s variables and then use them in flash. First we’re gona write a very simple XML file:

<?xml version=”1.0″ encoding=”utf-8″?>
<?xml-stylesheet type=”text/css” href=”Style.css”?>
<example Width=”590″ Height=”300″

</example>

First line defines encoding of the file. Variables that are important for us are put between tag “example” (this can be anything you prefer). We would like to read and assign variables width and height in flash.

In flash make a new AS 3 file. First we need to read XML file. We do that with following code:

var itemWidth:Number;var itemHeight:Number;

var myXMLLoader:URLLoader = new URLLoader();

myXMLLoader.load(new URLRequest("test.xml"));

myXMLLoader.addEventListener(Event.COMPLETE, processXML);

Please note that in this case xml file has to be in same directory as flash file. If you wish xml file to be in a subdirectory you have to state full path (like “subdirectory/test.xml”). First we define variables that will hold items that we define in XML and URL loader, then we load our file and finally we add an event listener that will start variable reading process which is done in function processXML:

function processXML(e:Event):void {var myXML:XML=new XML(e.target.data);

itemWidth = myXML.@WIDTH;

itemHeight = myXML.@HEIGHT;

}

First line in function defines variable myXML of type XML which we use for reading and assigning our variables from XML file. ItemWidth and itemHeight now hold values from XML file and are ready to use in your flash file.

Thank you for reading.