AS3: XObject Class - Treat objects like XML (E4X4O - "E4X for Objects")

I just finished this a few minutes ago (coincidentally while at a SilverLight camp ;)). Its an AS3 Object that allows you to add “elements” in the form of objects much like you would with XML using E4X. By assigning values to an XObject, you are adding that value into its “node” structure as one of its children with the given name. Retrieving a property, instead of returning a value, returns an instance of XObjectList which is essentially a list of objects (like XMLList) that are defined to that property. As with XML, there is a separation of elements and attributes using the @ character to precede references. Without @ you access a child, with, you are accessing a property or attribute of that particular XObject or whatever object defined inside it. Setting an attribute of an XObjectList, assigns the value to that property of all the objects in the list.

Its a little hard to explain. Its better to just see some examples.

var rootNode:XObject = new XObject();
rootNode.one = new Object();
rootNode.two = new Array();
rootNode.two = 20;
rootNode.two[1].deep = "Hello XObject";
rootNode.@three = "attribute";
trace(rootNode);

/* output:
<XObject three="attribute">
	<Object:one />
	<Array:two />
	<int:two>
		20
		<String:deep>
			Hello XObject
		</String:deep>
	</int:two>
</XObject>
*/
var rootNode:XObject = new XObject();
rootNode.a = 1;
rootNode.a = 2;
rootNode.a = 3;
delete rootNode.a[1];
trace(rootNode);

/* output:
<XObject>
	<int:a>
		1
	</int:a>
	<int:a>
		3
	</int:a>
</XObject>
*/
var myMcs:XObject = new XObject();
myMcs.badGuys = enemy1_mc; // movie clips on the screen...
myMcs.badGuys = enemy2_mc;
myMcs.badGuys = enemy3_mc;
myMcs.badGuys.@x = 100; // all enemy mc's move to x = 100

It’s pretty much done, but there might still be some problems, so if anyone finds any, just say so. I’m not sure how many more methods I might want to add in addition to what’s there now (to match XML) though I think I want to keep it as simple as possible, so probably not much more, if any, than what’s already there. To see what’s there now, you’d just have to check out the classes - its not much.