gskinner.com: gBlog: FlashMX 2004 source code, news, reviews and opinions
Back to gBlog Main gskinner.com website: source code, portfolio, etc. contact Grant Skinner

Syndication
RSS 1.0
RSS 2.0


Subscribe
Enter your email address to be notified when posts are added.

Search

Resources
Conference Session Notes

Flash Blogs
Waxpraxis
Philterblog
W3Blog
Jonas Galvez
Josh Dura
Quasimondo
Flashguru
Sean Voisen
Colin Moock
Flazoom
Greg Burch
Pope De Flash
Peter Hall
Glyn Thomas
actionscript.com
Princess Pegg


Aggregators
Flog
FullAsAGoog
MXNA
Hall of Justhese


« 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):
  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.

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/)

Posted by: iS at April 12, 2004 02:16 PM

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);
}


Posted by: Peter Cohen at April 23, 2004 10:40 PM

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.

Posted by: Grant Skinner at April 23, 2004 11:55 PM

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


Posted by: Bruce Pomeroy at April 29, 2004 06:37 AM

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

Posted by: Peter Cohen at May 25, 2004 10:38 PM

Peter, yes you may need to add

myXML.ignoreWhite = true;

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

Cheers.

Posted by: Grant Skinner at May 26, 2004 01:00 AM


Hosting by NetKeepers.ca | Powered by Movable Type 2.661
The text content of this blog is licensed under a Creative Commons License. Graphics are ©2003 Grant Skinner.