AWS WebService App

April 5, 2007

Here’s the AWS WebService Application I mentioned in my last post.

Thanks to e4x, i could query the AWS (Amazon Web Service), get books with the tag of “Flex” and populate them in a DataGrid (complete with itemRenderers and all) . The challendges i faced in the application were as follows

  • Understanding the way of using the required namespaces to access the result of the WebService. This was done using

private namespace AWSNS = “http://webservices.amazon.com/AWSECommerceService”;
use namespace AWSNS;

  • Since the data was coming in as e4x XML, the sort functionality of DataGrid was not working as expected. I needed to write sortCompareFunctions to get that to work.

So, here’s the AWS WebService App

and its source

Advertisements

E4X, My mx:WebService Buddy

April 5, 2007

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 🙂


What’s the Big Deal with E4X support in AS3

April 5, 2007

I couldn’t place the excitement in the team last year when we announced support for E4X in AS3. As a newbie in the XML arena, ignorant of the XML parsing woes in Flash & of terms like XPath, i really didn’t think its a big deal. My views have changed… a lot.

The traditional way of dealing with XML in Flash is using the The XMLDocument, XMLNode and XMLNodeType Classes (checkout the flash.xml package in the live docs). Say you have an XML as follows as myXML (in an XMLDocument Object)

<employeeList>
<employee id=”475″>
<lastName>Zmed</lastName>
<firstName>Sue</firstName>
<position>Data analyst</position>
</employee>
<employee id=”348″>
<lastName>McGee</lastName>
<firstName>Chuck</firstName>
<position>Jr. data analyst</position>
</employee>
</employeeList>

In legacy AS2, if you want to access the employee id of McGee i.e. 348, you need to access as :

myXML.firstChild.childNodes[1].attributes.id

If you want the firstname of McGee i.e. Chuck, you need to access as:

myXML.firstChild.childNodes[1].childNodes[1].firstChild.nodeName

Come AS3 & E4X and life becomes a walk in the park. Declare the myXML as an XML Object (native e4X xml type in As3)

You want McGee’s id ??? here it is

(myXML..employee)[1].@id

myXML..employee generates an XMLList (another E4X compatible class) by parsing the XML to generate only XML nodes with employee as the node (with its children). So myXML..employee generates

<employee id=”475″>
<lastName>Zmed</lastName>
<firstName>Sue</firstName>
<position>Data analyst</position>
</employee>
<employee id=”348″>
<lastName>McGee</lastName>
<firstName>Chuck</firstName>
<position>Jr. data analyst</position>
</employee>

To get McGee’s FirstName…

(myXML..employee)[1].firstName
OR (myXML..firstName)[1]

Also, you can run XPath2.0 queries on the resultant XML.For e.g., consider the following statement…

(myXML..employee.(@id>400)

It returns all employee nodes in the xml that has an employee id>400, which in this case is “Sue Zemed”. Cool ! E4X Rocks…

Check out the XML & XMLList classes in the livedocs. If you need more info on E4X, refer the following links