[AS3] XMLDefinition() - XML to Object (object definitions and hierarchies in XML)

XMLDefinition is a function that translates XML into ActionScript object definitions. It works a lot like Flex but translates definitions at runtime. As a runtime translater, its limited (no scripting etc) but still flexible in what it can be used to create.

Attached is the class and a simple Flash CS3 example. Below is some documentation. I’ll try to include a page with a live demonstration when I get a chance (and if I feel confident that the kinks have been ironed out).

Doc (from the AS file):
[size=5]XMLDefinition()[/size]

public function XMLDefinition(xml:XML, targetParent:Object = null):Object

Creates an object (typically a display object) based on the definition outlined by the XML. Each XML element represents the object to be made. If that object’s definition is in a pacakge, that element will need to be in a namespace whose uri matches that package. The hierarchy of the object created will match the hierarchy of the XML where display objects will be added as children to the display objects. Attributes are used to define properties. If a name attribute is defined, that name will also be used as a property name in the parent to store a reference to the child. If the parent is not a display object container or the child is not a display object and a name attribute is not provided, the child element’s index will be used; this is useful when defining arrays as name attributes are not needed.

There are 3 kinds of elements for special uses, each of which defined in the com.senocular.xml.XMLDefinition namespace. They are:

  • property
  • call
  • arguments

The property element is used to access an existing property of an object. For example, you may want to reference the transform property of a MovieClip instance defined in XML rather than redefine it. In that case you would use the property element to access that property’s existing value rather than provide a new value to be set for the MovieClip’s transform property. This element uses one attribute, name, to determine the name of the property being accessed.

The call element is used to call methods within objects. Like the property element, it too uses the name attribute to identify the class member (in this case a method) to be accessed. This would be useful for calling drawing methods on a display object. Method arguments are handled through the arguments element.

The arguments element is used in combination with class definitions or method calls. For class definitions (elements representing class instances) the arguments element contains the arguments for that instance’s constructor call. For method calls, the arguments element’s children are used as arguments for that method. The definitions within an arguments block is scoped to the parent node of the arguments element so references to property in an arguments element will reference properties in the argument’s parent object. One caveat concerning the arguments element is that you are limited to 10 or less arguments.

The following example creates a Sprite a line drawn in its graphics object and one Shape child object with the instance name “child”:

var definition:XML = 
<display:Sprite x="50" y="100"
	xmlns:display="flash.display"
	xmlns:def="com.senocular.xml.XMLDefinition">
	<display:Shape name="child" />
	<def:property name="graphics">
		<def:call name="lineStyle">
			<def:arguments>
				<Number>1</Number>
				<uint>0xFF0000</uint>
			</def:arguments>
		</def:call>
		<def:call name="lineTo">
			<def:arguments>
				<Number>100</Number>
				<Number>100</Number>
			</def:arguments>
		</def:call>
	</def:property>
</display:Sprite>

var sprite:Sprite = XMLDefinition(definition);

Parameters:
xml - The xml to be parsed into an object.
targetParent - The object in which the generated object will be defined, either as a property or a child display object.

Returns:
A reference to the object created.