Get XML definition from DisplayObject

February 3, 2011 by fMajakovskij

Since it is going to be an ongoing mini-project with a lot of updates, please refer to this link to see further update.

I always thought xml in AS3 is powerful to handle data and configuration.
But sometimes it is a pain in the ass design classes that have to be configured by xml files.

You have to implement every variables and relative methods to set them up properly, like:

private var _myVar:int=0;
public function setFromXML():void
{
     _myVar = int(xml.@myVar);
}

Today I’m introducing XMLDisplayManager class which is an attempt to make easier the class configuration.

Right now it has only one static method to get the xml definition from a passed DisplayObject.

Assuming we have the following code to create objects on Stage:

var s0:Sprite = new Sprite();
addChild(s0);
s0.x = 250;
s0.name = "baseClip";

var s1:Sprite = new Sprite();
s0.addChild(s1);

var s2:Shape = new Shape();
s1.addChild(s2);

var q:Bitmap = new Bitmap();
s1.addChild(q);
q.name = "bitmapClip"

// you need to create an empty xml object, since the getXmlFromDisplayObject
// function is recursive
var xml:XML = new XML(<stage />);

// Then pass the xml object and the clip you want to be dumped
XMLDisplayManager.getXmlFromDisplayObject(xml, s0);
trace(xml.toXMLString());

This is the generated xml from the above AS3 code:

<stage>
  <clip>
    <clip>
      <clip/>
      <clip/>
    </clip>
  </clip>
</stage>

Not much useful so far, but using an optional parameter in getXmlFromDisplayObject like:

// the third optional parameter is an array of
// properties you want to dump for each DisplayObject
XMLDisplayManager.getXmlFromDisplayObject(xml, s0, ["name", "x", "y"]);

Now we get the following xml

<stage>
  <clip name="baseClip" x="250" y="0">
    <clip name="instance2" x="0" y="0">
      <clip name="instance3" x="0" y="0"/>
      <clip name="bitmapClip" x="0" y="0"/>
    </clip>
  </clip>
</stage>

So far, so good.
In the next post I will illustrate how to use this xml dump to get something useful.

Download the XMLDisplayManager 001 class:
[download id="18"]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>