Yourpsp.com Wins 2006 Webby Awards AS2.0 - XMLDocument class revisited…

AS2.0 - XMLSerialiserAlpha

July 6th, 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.

can you Digg it? can you Digg it? is it del.icio.us? is it del.icio.us?

Category: General

Leave a Comment

You must be logged in to post a comment .

Trackback this post  |  Subscribe to the comments via RSS Feed