The XMLDocument class will help anyone who wants to save and load XML content and then handle it via a dot syntax object in Flash, there are a few deserialisers around, I wrote one myself a long while back (you can find it here.) but there's no solution I know of which wraps the whole process up in a neat little package... So that's the aim.
After thinking about the XMLDocument class (now called XMLSerialiserAlpha) a couple of things highlighted a new direction.
- The class should extend the XML class and wrap serialisation and de-serialisation.
- After looking at the problem of node attributes, 2 things came to mind.
- Properties named with a preceeding _ (underscore) could become attributes when serialising and vice versa for attribute names when deserialising.
- Properties could be placed in a container called __attributes.
I think option 2 is probably a better choice, since properties named with a preceeding _ (underscore) are common, and not always used for single value properties (as would be suitable for an attribute) and often contain other properties, collections or refer to objects. However, only providing some simple rules to object structures isn't adequate, so I decided that I'll need to create an object wrapper, accessible using dot syntax, but providing extra features which will assist with XML serialisation.
- CDATA sections could probably be identified on the fly, any text value which contains CDATA chars can simply be written out as a CDATA section, this will slow down the processing so I may make this optional, and add a facility to specifically tag a property as CDATA.
As a result, the next version of XMLDocument will not be compatible with the previous version, which was only a basic class anyway.
To reflect this I've amended the old code and named the class XMLSerialiserAlpha, if you're using it, I'd suggest you update your code to follow suit, make a note of the usage instructions in the class comments.
I'll tackle implementing the new class(es) in the coming week, I'll work out the specification of the Object wrapper next, let's call it DataObject, and this will allow safe creation and wrapping of data objects (containing no methods or MovieClips.)
This provides a nice place to formalise the object structure for XML serialisation, and a standardised format to work with for data XML files.
July 8th, 2006
This is a first draft solution to creating XML document from an Object structure (when E4X isn't available < flash9 / AS3) a friend of mine was asking if this was possible within Flash as a standard class, since it's not I thought about it and I decided it'd be quite easy to build, so I wrote this class based on Darron Schall's TraceObject function.
The class provides static methods to recurse an Object tree and create a corresponding XML document.
This is a rough first draft, proof of concept, which I'll try and improve over the next few days, there are a few things that I think it'll need, creating node attributes or CDATA sections from an Object, the thing is, these will require an Object to be structured in a particular fashion ... Something it'd like to avoid, but in some cases this functionality would be extremely useful.
Any suggestions for object formatting to provide CDATA / attribute notation would be well recieved.
Anyway here you go, an XML serialiser for Flash, it should work with all versions of Flash v6 up. (I've only tested it with Flash Player 8 & 9.)
/**
* A static helper class to create an XML document from an Object
* structure.
*
* @usage var myXMLDoc : XML = XMLSerialiserAlpha.seralise(myObject);
* Where myObject contains an object structure
* eg. http://en.wikipedia.org/wiki/JSON#JSON_example
*
* @author jason at mentalaxis dot com
*/
class XMLSerialiserAlpha{
private function XMLSerialiserAlpha(){}
private static var mInit : Boolean = false;
private static var mTabs : String = "";
private static var docString : String;
private static var xmlDom : XML;
private static function _out (strValue : String) : Void {
docString += strValue + "n";
}
private static function docStringToXML() : XML {
xmlDom = new XML(docString);
return xmlDom;
}
/**
* Recursively trace an object structure and send
* string fragments to the _out method, the user
* call returns an XML object.
*
* @param obj Object
* @param inOpenChar String
* @param initialBlockDisplayed Boolean
*
* @return the xml object to the external call
*/
public static function seralise(obj : Object, initialBlockDisplayed : Boolean, nodeName : String ) : XML {
mInit = (initialBlockDisplayed) ? false : true;
var topLeaf : Boolean;
if (mInit) {
docString = "";
mInit = false;
topLeaf = true;
} else {
topLeaf = false;
mTabs += "t";
}
for (var i : String in obj) {
if (typeof (obj [i]) == "string") {
_out (mTabs + "<"+i+">" + obj [i] + "</"+i+">");
} else if (typeof (obj [i]) == "object") {
if (obj [i] instanceof Array) {
seralise(obj [i], true, i);
} else {
if(nodeName != undefined) {
_out (mTabs + "<"+nodeName+">");
} else {
_out (mTabs + "<"+i+">");
}
seralise(obj [i] , true);
if(nodeName != undefined) {
_out (mTabs + "</"+nodeName+">");
} else {
_out (mTabs + "</"+i+">");
}
}
} else {
_out (mTabs + "<" + i + ">" + obj [i] + "</" + i + ">");
mInit = true;
}
}
mTabs = mTabs.substr (0, mTabs.length - 1);
if(topLeaf)
{
return docStringToXML();
} else {
return null;
}
}
}
Of course if you find any bugs or have suggestions for improvement, I'm interested to hear about them.
July 6th, 2006
I’ve been working on extending my flash based photo gallery, a few new features are now on the lastest gallery… Click Here to see
- The transitions now use a brightness blend, which I like very much, I think the way it blends the photos in and out from their light sources looks really cool.
- A tool tip now appears when you hover over the image navigation buttons, which displays the photo title… which is quite handy.
- I’ve added a background image too, which wasn’t a difficult thing but meant a preloader needed to be added… nothing too fancy just eye-candy.
I’ve been pondering the idea of adding scrolling for oversized images… some of my panoramic shots really need to be on the galleries properly. I’ve also been thinking about adding thumbnails, but I think maybe the tool-tip will be enough…
I’ve also extended the gallery to accept it’s object (image or swf) listings as an xml file, and this has meant it’s easy for me to turn the gallery into a mini-site engine for some of my low-low budget clients…
The system allows these guys to update their own content by modifying static images or SWF’s (animated or not) and updating the site XML file, which also controls things like an email gateway, audio clip or featured link.
June 14th, 2005