So you wanna build a Flex application - Part 3 - The Datasets

To date, in our series on Surfing Stats, we have covered the intent, directory structure and the main application file. (download the code using the download link at the bottom of the the Intro to Surfing Stats post). Now we will look at the datasets.

Surfing Stats consumes XML data representing popular stats categories from BlogCFC. If you look at The Stats page, you see many sections of stats. I wrote a quick mod that returns the data in XML form. I also normalized the data member names. You can see the raw data by clicking any of the categories below.

BlogTotals CategoryCount CommentedEntries CommentedCategories SearchTerms Commenters

Here is a sample of the data. This is the commented categories dataset.

view plain print about
1<?xml version="1.0" encoding="UTF-8"?>
2<query columns="3" rows="10">
3    <row>
4        <amount>203</amount>
5        <category>ColdFusion</category>
6        <categoryid>BACC89CE-F619-D127-03E75B6165D3CE1B</categoryid>
7    </row>
8    <row>
9        <amount>160</amount>
10        <category>Rapid Development</category>
11        <categoryid>2FC233D3-D690-0768-E4312419862F07AF</categoryid>
12    </row>
13    <row>
14        <amount>98</amount>
15        <category>Server Configuration</category>
16        <categoryid>E262C102-90C9-389A-6E3D0FD8688AB368</categoryid>
17    </row>
18    <row>
19        <amount>92</amount>
20        <category>Tutorials</category>
21        <categoryid>40B3092C-EF27-6D1D-1E3C1734114504E9</categoryid>
22    </row>
23    <row>
24        <amount>91</amount>
25        <category>Random</category>
26        <categoryid>BACDDF86-CB9C-2C46-423F985C8BFE1B4E</categoryid>
27    </row>
28    <row>
29        <amount>87</amount>
30        <category>Flex</category>
31        <categoryid>FDEB2819-9F27-DDC8-3C7C7A4B29BC8149</categoryid>
32    </row>
33    <row>
34        <amount>86</amount>
35        <category>Model-Glue</category>
36        <categoryid>40B2E264-09A3-6658-8DEC7ABEC55E8D2E</categoryid>
37    </row>
38    <row>
39        <amount>83</amount>
40        <category>Stupidity</category>
41        <categoryid>BACCB9D1-BCBB-8438-976CE3F4E6289E3C</categoryid>
42    </row>
43    <row>
44        <amount>39</amount>
45        <category>Personal</category>
46        <categoryid>BACD91D3-BCF8-D040-CDB8A750DC42321E</categoryid>
47        </row>
48    <row>
49        <amount>21</amount>
50        <category>Javascript</category>
51        <categoryid>364B5ACE-BAF7-F138-150B19DDB458F80E</categoryid>
52    </row>

We use this data to drive the table and charts in our application.

Requesting XML Data

Requesting XML data in Flex is easy.

view plain print about
1private function xmlDataHandler(event:ResultEvent):void
3    //the XML is Converted to an Array Collection by the result event
4    selectedDatasetAC = event.result.query.row;
5    //Define, configure amd run a numeric sort
6    var sort:Sort = new Sort();
7        sort.fields = [new SortField("amount", false, false, true)];
8        selectedDatasetAC.sort = sort;
9        selectedDatasetAC.refresh();
12<mx:HTTPService id="xmlLoader" result="xmlDataHandler(event)" />

The bulk of the code above is for creating a numeric sort and sorting the data. The true magic happens transparently. If you have ever parsed XML, you know that it isn't hard to walk down an XML tree and populate an object, it is just tedious. If you are a poor typer, like I am, it is also error prone.

Parsing the XML and Populating the ArrayCollection

In the Flex framework, the HTTPService makes the request to the server and then fires a result. The result automatically transforms the XML data into an ArrayCollection. Yay for Us! No boilerplate parsing or external libraries needed! The code above references event.result.query.row, row is the name of the repeatable node in our XML data.

Now that you have a good feel for the data that drives Surfing Stats, we can examine the application mode in depth. In the next series, we will dig into the main application file and look at the layout containers.

There are no comments for this entry.

Add Comment Subscribe to Comments

1/15/08 8:32 AM # Posted By Bruce

Dan - thanks again for posting this series of articles. I always learn something from studying other developer's Flex code.

For the mx:HTTPService tag you may want to specify a function to handle the fault event.