This is the third installment of the series, you can find the first two here:
Part 1 – http://blog.martinlegris.com/?p=87
Part 2 – http://blog.martinlegris.com/?p=90
Today I will develop on the data handling once the data is received from the webservice calls.
I will describe some methodologies I’ve developped that help mainstream data handling by using custom data types to hold the data, and custom iterators to access these data, and custom event types to transport the data.
In the last tutorial, we had a Singleton class calls WSClient which ran the request for us, then dispatched an event once the xml data was received.
Let’s look at a sample XML file returned by a theoretical webservice. The webservice returns a list of songs contained in a particular playlist. Here is the structure for each song:
First thing to do here, is to create a data object to hold this data.
Now let’s add getter/setters for the properties
Now let’s create a function which will convert the XML into our TrackData object.. I usually like to put this function inside of the data object itself, and make it static. I like to call it “fromXML”.
To use this function we would do like so :
And tada, we have “digested XML” to play with, a data object which is hard typed and easy to manipulate using auto-completion.
The Feed’s Signature
Now, since in this case we are receiving a list, or sequence of songs contained inside a playlist, it would be nice to have a custom iterator to let us easily access the songs. You can also put them in an array, but since arrays in AS3 are not type’able, (they will be in AS4), I like to create custom iterators that I call feeds. Our feed will have the following signature:
Basically, what I call a Feed is an Iterator that has been adapted to a specific data type, and which lets you iterate through a list of data objects. In this case, I use it to parse XML from a webservice response and enable easy access to that data.
XML Data to be parsed
Our werservice returns a playlist. Here is what the xml looks like
There are two techniques I’ve identified to handle the data inside of the Feed. Either transform all the data right away to custom data types, or keep the data as XML and convert it on demand. I will, in this example, show the second method. I has proven to cause problems on large data sets, for example when receiving 80k + XML files from the Amazon Web Services. So if you do have problems, just convert everything to AS3 objects and don’t keep a reference to the XML.
The Feed’s Implementation
As you can see, one of the class variables is _feed which is of type XML. Basically I keep the xml, and when needed I convert the data to TrackData for easy manipulation.
To use this feed, you would do like so :
As you see, it really easy! It requires a bit of coding up front, but down the line it’s much clearer and simpler to use than Arrays or using data straight out of the XML.
The Custom Event
Now, from the function inside of the WSClient, which handles the Loader's Event.COMPLETE event, it would be nice to fire an event to carry this feed to all interested parties. Here is the implementation of the event:
To dispatch this event from the WSClient:
That’s it for part 3 folks. In part 4 I will explain how to uniquely identify each request so that all multiple classes can use the WSClient singleton and ensure they do get THEIR results, and not the results asked for by another class.
Your tutorials are owesome sources for Flash knowledge. I’ve a little request that I’m sure I’m not the first one to ask you for: could you provide a link of all your packages of classes for dowload? I’m not able to make your code working, maybe because I don’t create the packages structure the right way.
Can you help?
THis is about a year late :), but I thought I should put up a reply. First of all. The tutorials are wonderful.
Now for the comment part :). There are two classes which you could have used to clean up the code a bit. XMLEncoder en XMLDecoder. Two nice classes that can do the marshalling and unmarshalling for you instead of that handcoded fromXML() function.