package com.ericfeminella.xml
{
import mx.rpc.xml.SimpleXMLEncoder;
import mx.collections.ArrayCollection;
import flash.xml.XMLDocument;
/**
*
* All static API which provides a utility for serializing an n-level
* nested <code>Object</code> to an <code>XML</code> encoded document
* structure.
*
* @see mx.rpc.xml.SimpleXMLEncoder;
*
*/
public final class XMLSerializationUtil
{
/**
*
* Serializes an n-level nested <code>Object</code> to an <code>XML</code>
* encoded document structure.
*
* @example The following example demonstrates how to encode an object to
* an <code>XML</code> structure using <code>XMLSerializationUtil</code>
*
* <listing version="3.0">
*
* var user:User = new User("foo", "passwordfoo");
* XMLSerializationUtil.serialize( user, "user" );
*
* // outputs:
* // <user>
* // <password>passwordfoo</password>
* // <username>foo</username>
* // </user>
*
* </listing>
*
* @param object in which to serialize to <code>XML</code>
* @param root node name to be used for the document root name
* @return <code>XML</code> object representation of object instance
*
*/
public static function serialize(value:Object, rootNodeName:String) : XML
{
var qname:QName = new QName( rootNodeName );
var document:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder( document );
if ( value is ArrayCollection )
{
value = ArrayCollection( value ).source;
}
simpleXMLEncoder.encodeValue( value, qname, document);
var xml:XML = new XML( document.toString() );
return xml;
}
}
}