11
Feb
09

Flex VO to XML Utility

This class takes a Value Object in actionscript and returns an XML Representation of that Object.

To use, create a new instance of the VOToXML class and call the toXML(); method to return the xml as as string. This method takes two parameters, the source value object and a string which represents the name of the root node in the xml.

This utility allows us to handle different types of properties within our VO. For example a VO property that is an ArrayCollection or Array must be handled to create a new set of child nodes.

package utilities
{
  import flash.utils.describeType;
  public class VOToXML
  {
    private var _df:MySQLDateFormatter = new MySQLDateFormatter();
    private var _tabIndex:int = 0;
    public function VOToXML()
    {
    }
    public function toXML(source:Object, rootName:String='root'):XML{
      var xml:String = '<'+rootName+'>\n'+handleProperties(source)+'</'+rootName+'>';
      //trace(xml);
      return XML(xml);
    }
    private function handleProperties(source:Object):String{
      _tabIndex++
      var xml:String = '';
      //This method relies on describeType  -- if class changes we'll need to update this helper
      var objectProperties:XML = describeType(source);
      //Walk through each property in object to build xml nodes
      for each(var property:XML in objectProperties.accessor){
        //Check to Make sure property values aren't null, if so use empty string
        //If property is a Date we convert to MYSQL format use the helper class MySQLDateFormatter
        if(property.@type == 'Date') xml += getTabs()+'<'+property.@name+'>'+(!source[property.@name] ? '' : _df.formatToMySQL(source[property.@name]))+'</'+property.@name+'>\n';
        //If property is Boolean we send a 0 or 1 in the xml
        else if(property.@type == 'Boolean') xml += getTabs()+'<'+property.@name+'>'+(source[property.@name] ? '1' : '0')+'</'+property.@name+'>\n'
        else if(property.@type == 'mx.collections::ArrayCollection' || property.@type == 'Array') {
          //xml+= getTabs()+'<'+property.@name+'>';
          if(source[property.@name]){
            //If we find an Array then we will walk through each object and its properties to build xml nodes, before moving on to the next property of the current object
            for each(var object:Object in source[property.@name]){
              xml += getTabs()+'<'+getSimpleName(object)+'>\n'+handleProperties(object)+getTabs()+'</'+getSimpleName(object)+'>\n';
            }
          }
          //xml+= getTabs()+'<'+property.@name+'>\n';
        }
        //Otherwise we use String in xml
        else xml += getTabs()+'<'+property.@name+'>'+(source[property.@name]=='' || !source[property.@name] ? '' : source[property.@name])+'</'+property.@name+'>\n';
      }
      _tabIndex--;
      return xml;
    }
    private function getSimpleName(source:Object):String{
      var objectProperties:XML = describeType(source);
      //Split on :: in name property take second item, which is object name
      var simpleName:String = String(objectProperties.@name).split('::')[1];
      //Look for VO if exists strip off and create lower case name
      if(simpleName.indexOf('VO')!=-1) simpleName = simpleName.substr(0, simpleName.length-2).toLowerCase();
      return simpleName;
    }
    private function getTabs():String{
      var tabs:String = '';
      for(var i:int=1; i<=_tabIndex; i++){
        tabs+= '  ';
      }
      return tabs;
    }
  }
}


1 Response to “Flex VO to XML Utility”


  1. 1 Qiana Steinmacher Nov 25th, 2011 at 9:40 pm

    We graphic this could be numerous upon the written content? then again We however take into account it generally works for just about any kind of matter material, as a result of it might steadily end up being pleasant to determine any warm and pleasurable encounter or even listen to the tone of voice while original obtaining.

Leave a Reply