When you start using the HttpService with the e4x outputFormat everything seems extremely simple. There is no complex XML parsing code and you can feed the results into a DataGrid for instance with almost no effort at all.
Simplicity has a price however and you realize you might need some more advanced features. In my case I wanted to have strong typed objects in the datagrid. This means a Date to be a date or a Number to be a Number.
Doing this is not actually complicated but finding out how it is. So here are some facts about the HttpService:
- there is a xmlDecode parameter which specifies a function to be called to decode the service result
- this function however is not called since the output format is e4x, the output format must be object
- the default implementation calls the SimpleXMLDecoder which does some type-age but then converts everything back to a ComplexString object in order to append the attributes of the XML element as properties
So what’s to do:
<mx:HTTPService ... resultFormat="object" xmlDecode="customDecode"><br></br><br></br>...<br></br><br></br>public static function customDecode(xml:XMLDocument):Object {<br></br> var xmlDecoder:CustomXMLDecoder = new CustomXMLDecoder(); <br></br> var result = xmlDecoder.decodeXML(xml); <br></br> return result;<br></br>}<br></br><br></br>//use the SimpleXMLDecoder as a template to create the CustomXMLDecoder,<br></br>//it can parse your XML and return what ever types of objects you need<br></br>
Alternate ideea
An alternate ideea is to use the class rpc/src/mx/rpc/xml/XMLDecoder.as which is used to decode WebService xml’s. However they are too complicated for my taste so implementing the custom decoder seems more beneficial for both the client and server side.