Monday, 21 June 2010

Parse XML in Actionscript 2

This is an older post I found on my computer that I forgot to upload back when I was using Actionscript 2. It’s a simple example of parsing XML in AS2, the code will basically display the attribute values from the XML to dynamic text fields on the stage.

Step 1

Open up your favourite text editor and type the following XML and save it as foods.xml into the same directory as your FLA file.

Step 2

Open a new AS2 file and select the Text tool with dynamic text and drag three text fields on the stage. Give the following instance names accordingly: name_txt, count_txt and type_txt.

Step 3

On the timeline insert a new layer called AS and then enter the following code into the Actions panel.
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
    if (success) {    
    var foodName  = myXML.firstChild.childNodes[0];
    var foodCount = myXML.firstChild.childNodes[0].attributes.count;
    var foodType   = myXML.firstChild.childNodes[0].attributes.type;

    name_txt.text = foodName;
    count_txt.text = foodCount;
    type_txt.text = foodType;


Step 4

Test your movie Ctrl + Enter and you should see the data from the XML displayed in the text fields.


  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP