|
« XML vs. AS2 - scoping callbacks |
Back to Main
| FlashForward update »
 |
February 27, 2004 |
 |
|
IMPORTANT NOTE: This is an old archive. It is only here to support outdated external links. To view the updated version of this archive, please go to the blog index, and search for the title of this document using the search form.
|
The ultimate AS2 XML solution!
Posted by Grant
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):
- It uses old-school Flash 5 style callbacks for its events.
- 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 com.gskinner.net.XML2;
myXML = new XML2();
myXML.addEventListener("load",this);
myXML.timeout = 3000;
myXML.load("http://1.1.1.1/nofile.xml");
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.
Posted @ 10:57 AM by Grant
| TrackBack
Comments
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/)
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);
}
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.
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
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
Peter, yes you may need to add
myXML.ignoreWhite = true;
to use the above example to load xml without the whitespace stripped out.
Cheers.
|