E4X, My mx:WebService Buddy

For an internal project (kindof), i had to use E4X to parse my results from a mx:WebService. I really found it a pleasure to work with it. The thing is that WebService class in Flex allows the return type to be “Object“, “xml” or “e4x“. The difference is as follows.

  • Using Object – Setting return type as Object, returns data in the form of an ObjectProxy. Below, I have provided the example of a WebService response from Amazon Web Service, inspected in FlexBuilder. As you can see, the ObjectProxy contains several complex datatypes, including ArrayCollection and mx.rpc.xml.ComplexString, which can be tricky to handle. Also you would have to do same parsing logic to parse the incoming result to your liking.

  • Using E4X : Makes it so much simpler. Gives me an XMLList as the result as below.

What this does for me is that the parsing becomes a cakewalk. I can just do the following and filter out all the Items in my result. res below is a ResutEvent of type mx.rpc.events.ResultEvent.

myXML:XMLList = res.result..Item;

Then, this XMLList variable can be bound to a mx:DataGrid (as DataGrid will internally convert it to an XMLListCollection) and you can work seamlessly with it… What a joy đŸ™‚

Advertisements

3 Responses to E4X, My mx:WebService Buddy

  1. You are totally right .. E4X is a boon for anyone dealing with xml data .. especially SOAP responses … I just love how it reduces the amount of work one has to do.

    And since we are on the subject of webservices with flex (a combo I use a lot at work, since SAP is all SOA) …. we know that Flex receives a response only when the request is a success and not in case of a fault … so what tool do you use to look at the SOAP message exchange?

    I use Ethereal (http://www.ethereal.com/) these days, but I’m not totally satisfied

  2. Pete says:

    I found this very helpful. As a flex beginner it would really help to see a more advanced tutorial on pulling in (document wrapped) web services and displaying them in list box or such.

    This is a very common use case, and there really isn’t much there on the web that has this example.

  3. dries says:

    I’m compiling in eclipse when i set an XMLListCollection as dataprovider for a control i get binding warnings like “XML is no eventDispatcher”.
    Are these warnings neglectable or should i make my own objectproxy value objects….
    (i dont want one way binding i need 2 way binding…)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s