0 Replies Latest reply on May 30, 2007 9:12 AM by Shivanil

    Exchanging Complex Data with Back-End Systems

    Shivanil
      Christopher has put an example to Convert AS Objects to XML Object in Flex1.5 and send them over to Server using HTTP See his code in < http://www.adobe.com/devnet/flex/articles/complex_data_02.html>. I upgraded the code for Flex2 using XMLDocument.
      XMLUtil.as::::::::::::::::::::

      package gmbs.gmbsservices.model
      {
      import mx.controls.Alert;
      import flash.display.Sprite;
      import flash.xml.XMLDocument;
      import flash.xml.XMLNode;
      import flash.xml.XMLNodeType;
      import gmbs.gmbsservices.model.ProductFilter;

      public class XMLUtil {

      public static function as2xml(obj:Object, nodeName:String):XMLDocument {
      var xml:XMLDocument=new XMLDocument();
      xml.appendChild(xml.createElement(nodeName));
      Alert.show( xml.toString(),'');
      for (var i:String in obj.Object) {
      Alert.show(obj.toString(), xml.toString());
      handleItem(xml, obj , i);
      }
      return xml;
      }

      private static function handleItem(xml:XMLDocument, item:String, nodeName:String):void {
      var type:String=typeof(item);
      if (type=="string" || type=="number" || item is Date) {
      var el:XMLNode=xml.createElement(nodeName);
      el.appendChild(xml.createTextNode(item));
      xml.firstChild.appendChild(el);
      } else if (item is Array) {
      var i:int;
      for(i=0; i<item.length; i++)
      handleItem(xml, item
      , nodeName);
      } else if (item is Object)
      xml.firstChild.appendChild(as2xml(item, nodeName));
      }

      }

      }


      ProductFilter.as (My AS Object)::::::::::::::::::::::::::::::::::::
      ////////////////////////////////////////////////////////////////////////////////
      //
      // Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
      // All Rights Reserved.
      // The following is Sample Code and is subject to all restrictions on such code
      // as contained in the End User License Agreement accompanying this product.
      // If you have received this file from a source other than Adobe,
      // then your use, modification, or distribution of it requires
      // the prior written permission of Adobe.
      //
      ////////////////////////////////////////////////////////////////////////////////
      package gmbs.gmbsservices.model
      {
      import flash.net.registerClassAlias;

      [RemoteClass(alias="gmbs.gmbsservices.model.ProductFilter")]

      [Bindable]
      public class ProductFilter
      {
      public var count:int;
      public var country:String;
      public var minAtmCount:Number;
      public var maxAtmCount:Number;
      public var corporate:Boolean;
      public var business:Boolean;
      public var multi:Boolean;

      public function ProductFilter()
      {
      super();
      }

      public function accept(product:Product):Boolean
      {
      //price is often the first test so let's fail fast if possible
      if (minAtmCount > product.atmCount || maxAtmCount < product.atmCount)
      return false;
      if (country != "All Countries" && country != product.country)
      return false;
      if (corporate && !product.corporate)
      return false;
      if (business && !product.business)
      return false;
      if (multi && !product.multi)
      return false;

      return true;
      }

      public function toXML():String
      {
      var out:String="";
      out = out+"<gmbs.gmbsservices.model.ProductFilter>\n";
      out = out + "\t<count>"+count.toString()+"</count>\n";
      out = out + "\t<country>"+country.toString()+"</country>\n";
      out = out + "\t<minAtmCount>"+minAtmCount.toString()+"</minAtmCount>\n";
      out = out + "\t<maxAtmCount>"+maxAtmCount.toString()+"</maxAtmCount>\n";
      out = out + "\t<corporate>"+corporate.toString()+"</corporate>\n";
      out = out + "\t<business>"+business.toString()+"</business>\n";
      out = out + "\t<multi>"+multi.toString()+"</multi>\n";
      out = out+"</gmbs.gmbsservices.model.ProductFilter>\n";

      return out;
      }


      }

      }

      I try to call the XMLUtil as ::::::::::::::::::::
      public function getFilterData(myObject:Object):void
      {
      //productXML= myObject.toXML();
      var pfDoc:XMLDocument = XMLUtil.as2xml(myObject, "ProductFilter");
      productXML=pfDoc.toString();
      filterService.send();
      }


      But XMLUtil can't convert this AS to XML it fails to do "for (var i:String in obj) {"
      This code works fine is the input Object is [object Object] but fails if input Object is [object ProductFilter].

      Any thoughts, did any cone converted ActionScript Objects/Classes to XML using similar code
      ?????????????

      Please help

      thanks
      Shivani