1 Reply Latest reply on Sep 19, 2007 1:52 PM by Peter deHaan

    ahow SimpleXMLDecoder  works ?

    xchg.ca Level 1
      Situation I have XML like that
      <album>
      <name></name>
      <images>
      <image></image>
      <image></image>
      <image></image>
      </images>
      </album>

      using HTTPService I'm getting this XML.

      I have AS Class:

      public class Album {
      public var name:String;
      public var images:ArrayCollection;
      }

      I try to do something like this

      public function result(event:ResultEvent):void {
      var xmlS:XML = XML(event.result);
      var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
      var xdoc : XMLDocument = new XMLDocument();
      xdoc.ignoreWhite = true; xdoc.parseXML(xmlS.toXMLString());
      var ob:Object = decoder.decodeXML(XMLNode(xdoc.firstChild));
      var feed:Album= ob as Album ;
      /* ob is instance of ObjectProxy(which has all required fields), but feed is null because it cannot be cast */
      }

      Is any suggestion around this, what I do wrong, may be there are some
      other way to do that
      any live example how to get AS object from XML using SimpleXMLDecoder will appreciated ;)
        • 1. Re: ahow SimpleXMLDecoder  works ?
          Peter deHaan Level 4
          voipsecdigest.com,

          Not sure if it gets you any closer to your goal, but here is a basic example of a SimpleXMLDecoder:
          [code]
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init();">

          <mx:Script>
          <![CDATA[
          import mx.rpc.xml.SimpleXMLDecoder;

          private function init():void {
          var xmlDoc:XMLDocument = new XMLDocument(albumXML.toString());

          var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();
          var albumObj:Object = decoder.decodeXML(xmlDoc.firstChild);
          nameTag.text += String(albumObj.name);
          img1Tag.text += String(albumObj.images.image[1]);
          }
          ]]>
          </mx:Script>

          <mx:XML id="albumXML">
          <album>
          <name>Bob</name>
          <images>
          <image>img1</image>
          <image>img2</image>
          <image>img3</image>
          </images>
          </album>
          </mx:XML>

          <mx:Label id="nameTag" text="name: " />
          <mx:Label id="img1Tag" text="images.image[1]: " />

          </mx:Application>
          [/code]

          Also, another cool trick is you can use the ObjectUtil.toString() method to easily trace out an object, which can be quite handy for debugging... For example, if you import the mx.utils.ObjectUtil class in the previous example and then add this line:
          [code]
          trace(ObjectUtil.toString(albumObj));
          [/code]

          You get the following output in your Flex Builder console (or log file):
          [quote]
          (Object)#0
          images = (Object)#1
          image = (Array)#2
          [0] "img1"
          [1] "img2"
          [2] "img3"
          name = "Bob"
          [/quote]

          Also, I dont know if it would be any easier or not for your example, but you could also parse the XML manually using E4X.

          Hope that helps,
          Peter