XMLToObjectSerializer
| Kind of class: | class |
|---|---|
| Inherits from: | none |
| Implements: | |
| Version: | 1.0 |
| Author: | Romain Ecarnot |
| Classpath: | com.bourre.data.libs.XMLToObjectSerializer |
| File last modified: | Wednesday, 31 October 2007, 07:35:29 |
XMLToObjectSerializer class defines tools to serialize any instance (of any type) into an XML structure.
Example
public function test() : Void { //build an object like we can have after XMLToObjectDeserialiser process var config = new Object(); config.log = new Object(); config.log.name = "Romain Ecarnot"; config.log.age = 29; config.log.male = true; config.log.detail = new Array("detail1", 2,3, false); config.log.subObject = new Object(); config.log.subObject.position = new Point(10,10); config.file = "config.xml"; config.xml = new XML("<article><item>hairbrush</item></article>"); var customInstance : TestClass = new TestClass(); config.custom = customInstance; var serializer : XMLToObjectSerializer = new XMLToObjectSerializer(); serializer.addType( ClassUtils.getFullyQualifiedClassName( customInstance ), this, _serializeTestClass); var xmlResult : XML = serializer.serialize(config, "myObject"); } private function _serializeTestClass(xml : XML, instance : TestClass, name : String) : XMLNode { var node : XMLNode = xml.createElement( name ); xml.firstChild.appendChild( node ); node.attributes["type"] = ClassUtils.getFullyQualifiedClassName( instance ); var param : XMLNode = xml.createElement( "name" ); param.attributes["type"] = "string"; param.appendChild( xml.createTextNode( instance.getName() ) ); node.appendChild( param ); return node; }
Summary
Constructor
Constructor
XMLToObjectSerializer
function XMLToObjectSerializer (
)
Constructs a new
XMLToObjectSerializer instance.Instance methods
addType
function addType (
classType:String,
parsingMethod:Function) : Void
Add custom parsing method for parsing specific instance type.
Example
var serializer : XMLToObjectSerializer = new XMLToObjectSerializer(); serializer.addType( ClassUtils.getFullyQualifiedClassName( test ), this, _serializeTestClass);
Take a look at #_parsePoint method to a custom parsing example.
Parameters:
classType :
String - Instance full qualified type (ie : com.bourre.structures.Point)scope :
Context where parsing method is defined (or must be called on)
parsingMethod:
Custom parsing
Functionserialize
function serialize (
instanceName:String) : XML
Serialzes passed-in
instance into XML structure.Use instanceName to named your object (optional)
Parameters:
instance :
Instance to serialize
instanceName:
(optional) Instance's name (name of first xml node)
Specified by:
toString
function toString (
) : String
Returns the string representation of this instance.
Returns:
the string representation of this instance