gSkinner - Home

The ultimate AS2 XML solution!

Posted on February 27, 2004 by Grant Skinner

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

Follow @gskinner on Twitter for more news and views on interactive media.
13 Comments

XML2 class with timeout and eventDispatcher

Posted by: RIADev on Feb 27, 2004 12:50pm URL: http://netmaths.net/RIADev/archive/2004/02/27…

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 on Apr 12, 2004 2:16pm

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 on Apr 23, 2004 10:40pm URL: http://www.arcmedia.com.au

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 on Apr 23, 2004 11:55pm URL: http://gskinner.com/

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 on Apr 29, 2004 6:37am URL: http://www.brucepomeroy.com

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 on May 25, 2004 10:38pm

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 on May 26, 2004 1:00am URL: http://gskinner.com/

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

Posted by: adam on Jun 20, 2005 6:47pm

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

Posted by: James Eberhardt on Jul 5, 2005 12:32pm

Hey, nice work. Really cool.

Adam - i think you need to use a '

Posted by: Pete Griffiths on Nov 25, 2005 4:23pm URL: http://www.kungfu-dmd.com

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

Posted by: Pete Griffiths on Nov 25, 2005 4:25pm URL: http://www.kungfu-dmd.com

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

Posted by: sanjeev rajput on Aug 8, 2007 11:49pm

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

Posted by: Shilpi Bharara on Sep 25, 2007 12:51am URL: http://www.flashonmind.net

Leave a Reply

Your email is never published nor shared.




You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">