Tuesday, March 22, 2011

Create a log application using XML and AIR: Part 1

In this tutorial we will learn how to use XML files with AIR and start creating an application that lets the user write and view logs, that are saved in an XML file.

Firstly, create a button called myWriteButton and an input text field called myText on stage. This is where the user will write his log entries to.

Here is how the code is going to work: we load data from an XML file as a string value, then parse it to an XML object and store it in a temporary local XML object. We do the changes we want to the local XML file, then, when we're ready to save, we simply overwrite the existing file with the new one.

Let's get started. Create a new File object with a reference to the XML file:

var myFile:File = File.documentsDirectory.resolvePath("Air Examples/mylog2.xml");

Create the local XML object:

var localXML:XML;

And set the text field to blank:

myText.text = "";

Now we need to update the XML object with the information from the real XML file, call the getXML() method:

getXML();

Now, the method itself. If the file we linked to exists - read from it and set localXML to that data. If it doesn't exist - create new one with starting and ending tags.

function getXML():void
{
if (myFile.exists)
{
var myStream:FileStream = new FileStream();
myStream.open(myFile, FileMode.READ);
localXML = new XML(myStream.readUTFBytes(myStream.bytesAvailable));
myStream.close();
}
else
{
localXML = <mylog></mylog>;
}
}

Add a listener for the button:

myWriteButton.addEventListener(MouseEvent.CLICK, writeEntry);

The function for the button appends a child to the local XML and updates the XML on the hard drive:

function writeEntry(evt:MouseEvent):void
{
localXML.appendChild(<logentry date={new Date()}>{myText.text}</logentry>);
myText.text = "";
saveXML();
}

To save the XML, we use saveXML() method, which overwrites the existing file or creates a new one if there isn't one already:

function saveXML():void
{
var myStream:FileStream = new FileStream();
myStream.open(myFile, FileMode.WRITE);
myStream.writeUTFBytes(localXML.toXMLString());
myStream.close();
}

Here's the full code:

import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.events.MouseEvent;
import flash.events.Event;
import fl.data.DataProvider;

var myFile:File = File.documentsDirectory.resolvePath("Air Examples/mylog.xml");
var localXML:XML;
myText.text = "";

getXML();

myWriteButton.addEventListener(MouseEvent.CLICK, writeEntry);

function getXML():void
{
if (myFile.exists)
{
var myStream:FileStream = new FileStream();
myStream.open(myFile, FileMode.READ);
localXML = new XML(myStream.readUTFBytes(myStream.bytesAvailable));
myStream.close();
}
else
{
localXML = <mylog></mylog>;
}
}

function saveXML():void
{
var myStream:FileStream = new FileStream();
myStream.open(myFile, FileMode.WRITE);
myStream.writeUTFBytes(localXML.toXMLString());
myStream.close();
}

function writeEntry(evt:MouseEvent):void
{
localXML.appendChild(<logentry date={new Date()}>{myText.text}</logentry>);
myText.text = "";
saveXML();
}

Next time we will learn how to display the logs in the application.

Thanks for reading!

Related:

Files and directories in AIR: Part 1
Files and directories in AIR: Part 2
Files and directories in AIR: Part 3
Files and directories in AIR: Part 4
Files and directories in AIR: Part 5
Files and directories in AIR: Part 6
Files and directories in AIR: Part 7
Files and directories in AIR: Part 8
Files and directories in AIR: Part 9
Files and directories in AIR: Part 10
Files and directories in AIR: Part 11
Create a log application using XML and AIR: Part 2
Create a log application using XML and AIR: Part 3
Create a log application using XML and AIR: Part 4
Files and directories in AIR: Part 12

3 comments:

Anonymous said...

Thank you very much! :D

Roberto Aprile said...
This comment has been removed by the author.
Roberto Aprile said...

Finally a save XML example that actually works! You save my day! Thank you very much!

Post a Comment