|
Bgx Xml Serialization Classes (Flash MX 2004) :
Serializing general complex objects
Flash output
This example corresponds with the file SerializationExample.fla
that is supplied with the component. The following code first creates
a custom complex object and then an instance of the BgxSerObject
class to serialize it using the default settings, displaying the
XML string created in a text field called tbxDisplay and
accesses the XML as XML to extract the label attribute
from the root element, displayed in the text field tbxRootLabel.
It furthermore sets the value of the text box tbxRootname as
the root name of the XML and the value of the text box tbxRootLabel
as the value of SampleData.label when the button btnSerialize
is clicked, and then re-serializes the data.
//import the class
import bgx.BgxSerObject;
//global variables to hold data
var SerObj:Object;
//create complex object
//create an array
var myArray:Array = new Array();
//add objects
for (var i = 0; i < 5; i++)
{
var obj = new Object();
//add attributes
obj["att1"] = "first value
- " + i;
obj["att2"] = "second value
- " + (i * 3);
myArray.push(obj);
}
//create root object and add attributes
var SampleData:Object = new Object();
SampleData["label"] = "Test object";
SampleData["childNode"] = myArray;
serialize();
function serialize(pRootName:String)
{
SerObj = new BgxSerObject();
if (typeof pRootName != "undefined")
{
SerObj.rootName
= pRootName;
}
SerObj.sourceObject = SampleData;
SerObj.onSerialized = setDisplay;
SerObj.serialize();
}
function setDisplay()
{
tbxDisplay.text = SerObj.xmlString;
var serXml:XML = SerObj.getXML();
tbxRootLabel.text = serXml.firstChild.attributes["label"];
}
btnSerialize.onRelease = function()
{
if (tbxRootlabel.text != "")
{
SampleData.label
= tbxRootlabel.text;
}
if (tbxRootname.text != "")
{
serialize(tbxRootname.text);
}
}
For further information please contact info@bgxcomponents.com.
|