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. ]