Marcos Placona Blog

Programming, technology and the taming of the web.

Category: Flex (page 1 of 2)

All the talks about Flex and my discoveries about this wonderful framework. Flex tutorials will also be posted

E4X and XML with namespaces

Reading time: 2 – 3 minutes

Here’s is something that got me scratching my head for a little while today while working on my new mobile application.

In my new application, I’ll be reading XML off various different providers, so I have an interface that gets implemented in various classes to make sure they all obey a certain contract, and I don’t need worry about what type they are (more on that later…)

With that in mind, I ended up implementing different logic on different classes since the XML returned will vary from provider to provider. I am using E4X to get the various information I need from the XML returned, and one of them would not work at all when selected.

I will put two XML examples here, and let you spot the difference:

var basket:XML =
	<foods>
	  <fruit>
		<name>Apples</name>
		<name>Bananas</name>
	  </fruit>
	</foods>;
trace(basket..fruit.name[0])

And that should return “Apples”.

Nothing new here, now for the second example:

var basket:XML =
	<foods xmlns="http://www.w3.org/TR/html4/">
	  <fruit>
		<name>Apples</name>
		<name>Bananas</name>
	  </fruit>
	</foods>;
trace(basket..fruit.name[0])

At a first glance, I have to admit I was expecting apples as well, but to my surprise, I got… nothing…

It took me a while to to go look on the XML again, and then it hit me. This provider would give me XML with a namespace, and in order to read that with E4X, I would need to declare that namespace. There is absolutely nothing wrong with the code above, but it simply won’t find anything since you’re not declaring what the namespace is, and the compiler gets completely lost.

What you need to do is declare the namespace on the top of my code like so:

namespace items = "http://ns.imageshack.us/imginfo/7/";
use namespace items;
trace(basket..fruit)

How you name it doesn’t matter, but it’s important that it’s unique, so in case you’re reading from multiple XML files on the same class, you should be using different names, otherwise you will get compiling errors.

SQLite databases and mobile applications – A caveat

Reading time: 2 – 4 minutes

As some of you might have noticed, I have been building some mobile applications lately on my spare time specifically for the Blackberry Playbook. They are mainly built in Adobe Air using Actionscript 3 and Blackberry’s Tablet SDK.

On my latest application, I have found the need for a database, as it needs to store lots of user input data. The first option that comes to mind is SQLite, as it’s very simple to integrate, and has native support.

I will show here an integration example, and focus on a caveat I found while trying to use it on a real device.

For starters, here are the classes you need to import on your main class:

import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.filesystem.File;

I then create a method to initialize my database connection:

private var dbConnection:SQLConnection = new SQLConnection;
private function initDB():void{
	var embededSessionDB:File = File.applicationDirectory.resolvePath("assets/db.sqlite");
	var writeSessionDB:File = File.applicationStorageDirectory.resolvePath("assets/db.sqlite");
	// If a writable DB doesn't exist, we then copy it into the app folder so it's writteable
	if (!writeSessionDB.exists) {
		embededSessionDB.copyTo(writeSessionDB);
	}
	var dbFile:File = writeSessionDB;
	dbConnection.open(dbFile);
}

What is important to mention here, is that most people (myself being one of them) will be completely tempted to skip the bit where it copies the local file (coming from the application itself) into the local storage. Funnily enough, if you skip this step, when you test it locally, it will all work wonderfully, but when you deploy it into a real device (or even a simulator) nothing happens at all.

After some research, I found out that you need to be using the database from the local storage, and just like this is that you will have read and write permissions.

After getting that small “detour” out of the way, I can go on and write my first query against that database.

private function getWords(intTabID:int):Array{
	var stmt:SQLStatement = new SQLStatement();
	stmt.sqlConnection = dbConnection;
	stmt.text = "SELECT key, word FROM WORDS WHERE category_id = (:wordID)";
	stmt.parameters[':wordID'] = intTabID;
	stmt.execute();
	var result:Array = stmt.getResult().data;
	return result;
}

And this will return a nice array with all my data from the SQLite database.

Quick tip on Flash Builder and application internationalization

Reading time: 1 – 2 minutes

I’m building a new Blackberry Playbook app, and when starting to work with internationalization, I got stuck with a bug that wouldn’t go away. I’m pretty sure this won’t just affect just this kind of application, but anything that is built using Flash Builder and requires resource bundle files.

It’s a pretty common standard to name resource bundle files as *.properties, but it turns out Flash Builder ignores *.properties files when packaging, and you end up with an error like:

Error: Error #3003: File or directory does not exist.

It actually took me quite a long time to figure that one out, since I was pretty sure the files were in the correct location. Changing the file extension to .prop did the trick, and now Flash Builder will package them successfully.

I believe there must be another way in Flash builder settings to make it not ignore *.properties files, but for the time being, simply renaming its extension will do.

Hope that helps someone in the future searching for this error.

First Flex 4 book released

Reading time: < 1 minute

The first Flex 4 book (Hello! Flex 4) has just been released this month in the UK. I haven’t read it yet, so no way I could give any reviews on it, but from the interwebs, I can see it’s contents are really good. A must buy for the festive period.

Date Formatting with Flex

Reading time: 2 – 3 minutes

Formatting dates with flex
(Photo: adactio)

I’ve been working with some Adobe Flex recently, and was having some trouble formatting dates. For those who don’t know, here (in th UK) we use the format “dd/mm/yyyy” (Day/Month/Year) as opposed to people over in the States using “mm/dd/yyyy” (Month/Day/Year).
I had to use the DateField component, and couldn’t find a way in heaven to get the date it returns formatted as “dd/mm/yyyy”.
I consider myself a flex beginner, and haven’t really done much with it (I know, shame on me), and usually have research for online resources (A.K.A “Google Desperation”).
Surprisingly, it’s not very easy to find good online resources for Flex, and when searching for it, I was mostly presented with dodgy date strings being split up and used as arrays.
Damn it! Isn’t there anything like Coldfusion’s DateFormat?
I then had to resource to twitter, and quickly got the answer as if in a water stream!
Basically what you have to to, is use mx:DateFormatter, and create your mask as such:

<mx:DateFormatter id="formatDate" formatString="DD/MM/YYYY" />

You can now use this formatter in order to format any date you might have as a string:

<mx:Script>
     <!--[CDATA[
     public var myDate:Date = new Date;
     trace(formatDate.format(myDate));
     ]]-->
</mx:Script>

And this will format your date as “dd/mm/yyyy”. I hope this is useful to somebody else some other time.

Older posts