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):
Here's what it looks like in use:
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. ]
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.
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 Flash, Flex, and ActionScript.

Comments (12)
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 PMURL:
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) {
Posted by: Peter Cohen at April 23, 2004 10:40 PMsuper(p_xml);
this.ignoreWhite = true;
EventDispatcher.initialize(this);
}
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,
Posted by: Grant Skinner at April 23, 2004 11:55 PMGrant.
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 at April 29, 2004 06:37 AMURL: 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 at May 25, 2004 10:38 PMURL:
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 AMURL: 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 at June 20, 2005 06:47 PMURL:
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 at July 5, 2005 12:32 PMURL:
Hey, nice work. Really cool.
Adam - i think you need to use a '
Thanks
Pete
Posted by: Pete Griffiths at November 25, 2005 04:23 PMURL: 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
Posted by: Pete Griffiths at November 25, 2005 04:25 PMPete
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 {
}
}
Posted by: sanjeev rajput at August 8, 2007 11:49 PMEnjoy
sanjeev
URL:
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 at September 25, 2007 12:51 AMURL: http://www.flashonmind.net