In theory

Actionscript 3.0’s XML model is based on this Ecma-357 (version 2) standard (here is the PDF download). It’s long and boring, which sucks because it is very educational in terms of what you can do in AS3. Adobe follows the standard pretty closely, so this can actually substitute for much of the Adobe documentation.

Sometimes some examples help, and Adobe is very useful for finding usage samples. The XML demo code (and Adobe’s XML documentation in general) is found at AS3’s docs XML reference: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/XML.html#includeExamplesSummary

Also, don’t miss Kirupa’s prodigious efforts: Using XML in Flash CS3/AS3

Of note

  • Non-intuitive in the Adobe example is that the first element in an XML assignment to a variable is that variable. So with this code:
var refVar:XML = <test><setup><title>title</title></setup></test>;

refVar takes the place of and the xml data is accessed by refVar.setup, and not refVar.test.setup.

  • Though you can loop using for-each syntax, I wanted an index number. (Eventually) I figured out that this works:
//var val:uint = xmlData.questions.question.length;
var val:uint = xmlData.questions.child("*").length();
for (var i:uint = 0; i < val; i++)
{
    trace("TriggerArray.quizXMLToDS: " + i + ": " + xmlData.questions.question[i].@id);
}

In Practice

This example loads and prints an XML file. This is the basically same method that Kirupa uses. I like to dispatch an event after the XML is fully loaded, too, since I use this asynchronously, and need to wait until whatever post-load, pre-continuation data is done. So to use this example, you would listen for the XML_LOADED event:

import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

public class Test extends MovieClip
{
    public static const XML_LOADED:String = "XML_LOADED";

    private var xmlUrlReq:URLRequest;
    private var xmlLoader:URLLoader;
    private var xmlData:XML;

    public var isOKToLoadSO:Boolean;  // This should really use a getter-setter. . .

    private function loadXML():void
    {
        isXMLGood= false;
        xmlLoader = new URLLoader();
        xmlUrlReq = new URLRequest("file.xml");

        // Listen for file load.
        xmlLoader.addEventListener(Event.COMPLETE, xmlFileLoadedHnd, false, 0, true);

        // After something has been done to the XML data, XML_LOADED event will fire, so listen for that event, too.
        this.addEventListener(XML_LOADED, xmlIsCompleteHnd, false, 0, true);

        xmlLoader.load(xmlUrlReq);
    }

    protected function xmlFileLoadedHnd(e:Event):void
    {
        // Reference the XML data from the target.
        xmlData = new XML(e.target.data);

        // Show the XML data.
        trace(xmlData);

        // Do something to the XML data
        // and set a synchronous (linear) condition variable.
        isXMLGood = doSomethingToXMLData(xmlData);

        // Notify that everything is set up correctly
        // by firing an asynchronous event.
        dispatchEvent(new Event(XML_LOADED));
    }

    private function doSomethingToXMLData(xmlData:XML):Boolean
    {
        // Do something to the XML data here.
        return true;
    }

    protected function xmlIsCompleteHnd(e:Event):void
    {
        // Continuation code goes here.
    }
}

– 30 –

Advertisements