The ultimate AS2 XML solution!

Yesterday I posted a way to maintain scope inside of an XML callback. Today, I decided to take it one step further, and build out the XML object I always wish for (partly inspired by this post by Keith Peters at Bit-101 that someone pointed me to after I made my post yesterday). The sad thing is, it took me about 30 minutes to code – if I had done this 6 months ago, I could probably have saved myself hours of work. Procrastination sucks.

There are 2 major features gaps in the current XML object as I see it (well 2 that I feel like addressing, anyway):


  1. It uses old-school Flash 5 style callbacks for its events.

  2. It doesn’t ever time-out, so you have to write elaborate code constructs to keep track of your XML requests (this goes for loadvars and loadMovie too, but one thing at a time), and make sure they don’t take too long.


In order to address these, I created XML2 (that’s not just a lazy name, it’s XML for AS2), which extends XML, implements eventDispatcher to broadcast it’s load event (no support for onData yet), and adds a timeout property that lets you specify an interval in milliseconds after which to timeout on the request (7000ms by default). It’s even compatible with gDispatcher (if only I was allowed to distribute it, dang EULA).

Here’s what it looks like in use:

// import the class
import com.gskinner.net.XML2;
myXML = new XML2();
// subscribe to its load event:
myXML.addEventListener("load",this);
// set the timeout period to 3 seconds (default is 7s):
myXML.timeout = 3000;
// this should timeout after 3 seconds:
myXML.load("http://1.1.1.1/nofile.xml");
// function to handle the load event:
function load(p_evtObj:Object):Void {
var XMLObj:XML = p_evtObj.target;
trace("success: "+p_evtObj.success);
trace("status: "+XMLObj.status);
trace("toString: "+XMLObj.toString());
}

You can download the XML2 class by clicking here. Happy coding.

[ UPDATE (2005.11.25): I have posted a new version of XML2 that adds support for Flash Player 8 and the new onHTTPStatus event. Click here for details. ]

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

14 Comments

  1. XML2 class with timeout and eventDispatcher

  2. This is a welcome addition, I’ve spent some time trying to figure out the best OOP way to import XML and this helps a lot.

    I’m currently trying to mix XML2 with XPath (http://www.xfactorstudio.com/Actionscript/AS2/XPath/)

  3. Nice work! I don’t know how you get on, but I had to alter the constructor to ignore whitespace:

    function XML2(p_xml:String) {

    super(p_xml);

    this.ignoreWhite = true;

    EventDispatcher.initialize(this);

    }

  4. Peter,

    it extends XML, so you can use it exactly the same as XML:

    myXML = new XML2();

    myXML.ignoreWhite = true;

    myXML.load(“blah.xml”);

    Cheers,

    Grant.

  5. Nice work. Earlier today I came up with this. It’s another way to ‘update’ XML for AS2 style event model, for your interest:

    /////////////////////////////////////////////

    // set the XML objects onLoad method to undefined,

    // else UIEventDispatcher wont add load events:

    myXML.onLoad = undefined;

    mx.events.UIEventDispatcher.initialize(myXML);

    myXML.addEventListener(‘load’, this);

    /////////////////////////////////////////////

    cheers

    bruce

  6. Thanks Grant,

    I realised, but why doesn’t your example? If I put your example together with XML2, it doesn’t work — without altering one or the other. Or perhaps it’s…

    Do you use whiteless xml?

    🙂

    peter

  7. Peter, yes you may need to add

    myXML.ignoreWhite = true;

    to use the above example to load xml without the whitespace stripped out.

    Cheers.

  8. I have got this working great – but none of my HTML tags are converted in my text area component , even tho I have set myText.html = true ? any ideas

  9. James Eberhardt July 5, 2005 at 12:32pm

    Hey there; great stuff Grant!

    I was wondering if you could post an example of what the next steps would be for handling the XML. Basically, once you have it downloaded, how do you handle parsing it? What event triggers that?

    Basically, I have a number of XML files that I’d like to download and parse before the movie loads.

    Any suggestions?

    James

  10. Hey, nice work. Really cool.

    Adam – i think you need to use a ‘

  11. ok, so it cut that last message, it was mean to say a ‘CDATA’ tag, take a look here:

    http://www.w3schools.com/xml/xml_cdata.asp

    Cheers

    Pete

  12. sanjeev rajput August 8, 2007 at 11:49pm

    Here is some good option to load multiple XML file

    /***************************************************************************************

    * Author – Sanjeev Rajput

    * Date – 16-July-07

    * class is used to load any XML file and dispatch an event when XML is loaded

    *****************************************************************************************/

    class XMLLoader extends mx.events.EventDispatcher {

    //———— variable Declaration —————–

    public var isLoaded:Boolean = false;

    private var xmlObj:XML;

    private var fileRef:String;

    private static var ref:Object;

    //———- constructor function ———

    function XMLLoader() {

    trace(‘XMLLoader constructor called’);

    this.xmlObj = new XML();

    this.xmlObj.ignoreWhite = true;

    }

    //———- XML load function —————-

    public function loadXML(fileRef,param:String) {

    this.fileRef = fileRef;

    ref = this;

    this.xmlObj.onLoad = function(SS:Boolean) {

    if (SS) {

    //———— dispatch event when loading complete ————–

    ref.dispatchEvent({type:”XMLLoaded”, target:this, targetName:param});

    } else {

    return;

    }

    };

    //————— xml file reference ————————

    this.xmlObj.load(this.fileRef);

    }

    }

    Now we can use this custom class to load XML file in any other .as file like this….

    /***************************************************************************************

    * Author – Sanjeev Rajput

    * Date – 16-July-07

    * Version 1.0

    *****************************************************************************************/

    class UIController extends mx.events.EventDispatcher {

    private static var UI_objRef:Object

    private var WareaXML_obj:Object;

    private var WareaXML:XML;

    private var WareaXMLpath:String = “data/Warea.xml”;

    private var GalleryXML_obj:Object;

    private var GalleryXML:XML;

    private var GalleryXMLpath:String = “data/Gallery.xml”;

    private var fontListXML_obj:Object;

    private var fontListXML:XML;

    private var fontListXMLpath:String = “data/FontList.xml”;

    private static var isXMLParse:Boolean = false;

    //———- constructor function ———

    function UIController(timeLine) {

    UI_objRef = this;

    this.WareaXML_obj = new XMLLoader();

    this.WareaXML_obj.loadXML(this.WareaXMLpath, “WareaXML”);

    this.fontListXML_obj = new XMLLoader();

    this.fontListXML_obj.loadXML(this.fontListXMLpath, “FontListXML”);

    this.GalleryXML_obj = new XMLLoader();

    this.GalleryXML_obj.loadXML(this.GalleryXMLpath, “GalleryXML”);

    this.GalleryXML_obj.addEventListener(“XMLLoaded”, this.initXMLData);

    }

    private function initXMLData(evt):Void {

    if (evt.targetName == ‘GalleryXML’) {

    UI_objRef.GalleryXML = evt.target;

    UI_objRef.GalleryXML_obj.removeEventListener();

    UI_objRef.initGComboBoxData();

    }

    if (evt.targetName == ‘WareaXML’) {

    UI_objRef.WareaXML = evt.target;

    UI_objRef.GalleryXML_obj.removeEventListener();

    UI_objRef.initWComboBoxData();

    }

    if (evt.targetName == ‘FontListXML’) {

    UI_objRef.fontListXML = evt.target;

    UI_objRef.GalleryXML_obj.removeEventListener();

    UI_objRef.initFComboBoxData();

    }

    }

    private function initWComboBoxData():Void {

    }

    private function initGComboBoxData():Void {

    }

    private function initFComboBoxData():Void {

    }

    }

    Enjoy

    sanjeev

  13. Hi

    I have a problem with xml and have no solution to it.I know it is not reated to this article but…

    My flash file loads an xml file. When the user opens the flash file(online), it gets downloaded on the user’s temporary internet files.Also the xml file gets downloaded on the machine. In this way all my content reaches the clients’ machine.I want to prevent that.Is there any solution to that?

    Is there any way od passing xml data to flash file without the file getting downloaded on client’s machine.

    My problem is NOT related to caching of xml file(which most of the people think as).My problem is that I do not want my content files to reach the client’s machine.

    Eagerly waiting for some support…

    Shilpi

  14. Sylvain Lecoy June 8, 2012 at 7:02am

    @Shilpi “My problem is that I do not want my content files to reach the client’s machine.”

    The solution to that problem is straightforward. Remove it from the server if you don’t want your content to reach the client’s machine.

    If at some point you need that a client use your content, it will be downloaded, so I’m afraid that you are looking for something not physically (even not conceptually) possible.

    Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *