Here's a snippet from my XMLUtils class that converts an XML list of
"companies" to an ArrayCollection of Company objects (relies on Darron
Schall's ObjectTranslator tools for convenience):

/******************************************************
* Convert from XML List to Company ArrayCollection
******************************************************/
public static function
companiesXMLDecoder(xml:XMLDocument):ArrayCollection {
     var companyCollection:ArrayCollection = new ArrayCollection();
     var xmlDecoder:SimpleXMLDecoder = new SimpleXMLDecoder();

     if (xml.firstChild.childNodes.length > 0) {
         var objectTree:Object = xmlDecoder.decodeXML(xml.firstChild);
         var companies:Array;

         if (objectTree.company is Array) companies = objectTree.company;
             else companies = new Array(objectTree.company);

         for (var i:int=0; i < companies.length; i++) {
             var company:Company =
ObjectTranslator.objectToInstance(companies[i], Company) as Company;
             companyCollection.addItem(company);
         }
     }
     return companyCollection;
}


----- and now the HTTPService definition ----


<mx:HTTPService
     id="getCompaniesService"
     url="http://someserver/getCompanies.xml";
     showBusyCursor="true"
     resultFormat="object"
     xmlDecode="XMLUtil.companiesXMLDecoder"
     result="getCompaniesResultHandler(event)"
     fault="getCompaniesFaultHandler(event)">
</mx:HTTPService>


... the event.result received by getCompaniesResultHandler will be an
ArrayCollection of Company objects.  =]




--- In flexcoders@yahoogroups.com, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> 
wrote:
>
> I'd love to see an example, we were about to parse the XML and create
our own array objects with the correct types for binding but this sounds
like a much cleaner method.
>
> Cheers,
> Grant
>
> ----- Original Message -----
> From: brent_trx [EMAIL PROTECTED]
> To: flexcoders@yahoogroups.com
> Sent: 1/31/07 4:27 PM
> Subject: [flexcoders] Re: Flex 2 HTTPService best practices
>
> > I was facing a very similar issue just recently. I had all these AS
> > model classes that mapped identical properties in accordance to the
> > backend objects I was dealing with, but no FDS-style convenience
> > mechanism to handle typing via HTTPService calls. What I found out
is
> > the beauty of the "xmlDecode" and "xmlEncode" methods.
> >
> > When you use the xmlDecode method, passing it a function reference,
> > that function will expect an XMLDocument object that is generated
> > automatically by the service. You can then take that XMLDocument,
> > iterate through it, building and returning objects of whatever type
> > you desire. When you access "event.result" in your handler function,
> > that property value will be typed based on your decoder return type.
> > So "event.result" is now, say a User object, rather than a user xml
> > node representation.
> >
> > The beauty of this strategy is, at any point, your transport
mechanism
> > becomes disposable. If you suddenly decide to switch from XML to
JSON
> > as your transport mechanism of choice, you only need to code some
> > decoder / encoder functions, and you're done. No need to change
what,
> > say, a datagrid expects. Very elegant architecture.
> >
> > I recommend taking a look at AS3's SimpleXMLDecoder class, along
with
> > Darron Schall's ObjectTranslator class, which makes writing an XML
> > decoder a breeze.
> >
> > If anyone is interested, I can post a sample.


Reply via email to