E4X to Object to Array and back again

Recently when working on the Filtrbox Olympic AIR widget, I ran into some trouble in reading and writing preference in AIR as XML. There was not very much information concerning the topics so i felt i would give some quick insight. I am sure there are better answers, but this worked for me. This write- up assumes that you already know how to create, and read XML data from AIR, but you are having hard time with tubular data. This app is designed around RSS consumption, so we will create a Value Object called FeedVO that looks like this (Using the Cairngorm Micro Architecture):

package com.filtrbox.widget.vo{
import com.adobe.cairngorm.vo.IValueObject;

[Bindable]
public class FeedVO implements IValueObject{
public var feed:String;
public var name:String;
public var type:String;
}
}

Alright, with the VO setup, its time to read in preferences. First, lets set up a reference to the XML object we will be using, called “applicationSettings”.

[Bindable] public var applicationSettings:XML;

Now lets create the XML scheme that we will use to add data to and create the default structure. This same structure that is also used to process the data.

public function createXMLSchema():Void{
applicationSettings = <preferences/>;
var beijing:XML = <beijing/>;
for each(var feed:FeedVO in model.allFeeds){
beijing.@name = feed.name;
beijing.@feed = feed.feed;
beijing.@type = feed.type;
applicationSettings.appendChild(beijing);
beijing = <beijing/>;
}
writeXMLData()
}

Lets now look over what we have done. We have created two nodes - preferences and beijing. Since we are using E4X, we can use node values like “feed” and “type”. In the model, an Array Collection has been created that will contain all feed information and thus is called “allFeeds”. So for each value object in the “allFeeds” array, we will recurs and append the result to a new beijing node. After the last result has been added, we will want to write the data to the XML file.

private function writeXMLData():void
{
var outputString:String = '\n';
outputString += applicationSettings.toXMLString();
outputString = outputString.replace(/\n/g, File.lineEnding);
stream = new FileStream();
stream.open(applicationFile, FileMode.WRITE);
stream.writeUTFBytes(outputString);
stream.close();
}

When reading the preferences, we will follow the same structure:

private function processXMLData():void
{
applicationSettings = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
for each(var beijing in applicationSettings.beijing){
var feed:FeedVO = new FeedVO;
feed.name = beijing.@name;
feed.feed = beijing.@feed;
feed.type = beijing.@type;
model.allFeeds.addItem(feed);
}
if(model.allFeeds.length<=0 || model.allFeeds == null){
var defaultFeed:FeedVO = new FeedVO;
defaultFeed.name = 'Olympics 2008';
defaultFeed.type = 'event';
defaultFeed.feed = 'lEpq5xVyDq5UtjtNOboOyQ==';
model.allFeeds.addItem(defaultFeed);
}
}

When reading the preferences, we will recurs the biejing node and separate each value to add as an object. Once we have created the object, we will add it to our “allFeeds” array in the model. We also want to handle a first time use case where there are no preferences and load a default feed, which is handled by the second “if” statement. So I quickly showed you how to recurs tubular data in order to read or write to and XML file. When you are using the AIR environment, you may also want to use SQLite, which will follow a similar format in how you handle the objects.

This entry was posted on Monday, July 28th, 2008 at 8:01 pm and is filed under Adobe Flex. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

 

Leave a Reply