19 November 2008 ~

VO to XML converter

The following code converts a value object to an XML

private function convertToXML():void
{
var product:ProductVO = new ProductVO();
product.id = 12;
product.name = ‘test product’;
product.description = ‘test desc’;
product.price = 100;
product.thumbnail = ‘image1.jpg’;
var xmlProduct:XML = voToXml(product, ‘product’);
}

private static function voToXml(vo:Object, qName:String):XML
{
var qn:QName = new QName(qName);
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(vo, qn, xmlDocument);
var xml:XML = new XML(xmlDocument.toString());
return xml;
}

Tags: , , ,

One Response to “VO to XML converter”

  1. Gordon 2 March 2009 at 7:49 pm Permalink

    Hi.

    This is very useful code. Thanks for posting it.

    Just a small question. Do you know of a convenient method to convert a VO to an XMLListCollection and use all the properties to populate an mx:Tree component or datagrid component?

    The problem I see it that I would like to populate a tree component but don’t know all the properties ahead of time and was wondering if the QName could be used to populate the “labelField” property of the mx:Tree component.


Leave a Reply