Introduction

I have decided to play a bit with XML in flash while helping a friend to make two movies exchange informations. XML is a standard way of exchanging data and it seemed a good way for making the movies depend one of eachother.

After searching the net and gathering pieces of informations from there and there, I decided to put them all toghether and come up with a basic example that
shows how things work.

Description

Flash is not allowed to write files on disk (it runs from the browser, and is only allowed to communicate with the server it was loaded from). Hence, when you need informations to be saved on the remote server, you need to have a script on the server, a script which you will invoque and pass along the XML via a POST request. The script takes care of putting the XML on the disk, from where it can be loaded later on by some other movie.

Details

I have created two movies, EnterValue and Display (the first one will redirect you to the next one; for source code please check the “Source Code” at the end of the page).

EnterValue is a simple form having a TextInput and a Save button. If you press the Save button then the XML is composed and sent to the PHP script found on the server.
Display just loads the XML which the php script has saved and displays the value stored inside.

As the example is a basic one, and there is only one value to store in XML, the XML structure is simple. There is a “root” node having a single child called “value” with the contents from the TextInput.



10

EnterValue

The “Save” button has this code for the click event:

on(click)
{
var myxml:XML = new XML();
root = myxml.createElement(“root”);
element = myxml.createElement(“value”);
textValue = myxml.createTextNode(this._parent.mytextbox.text);
element.appendChild(textValue);
root.appendChild(element);
myxml.appendChild(root);
myxml.contentType = “text/xml”;
myxml.send(“
http://hex.ro/files/flashxml/save.php“, “POST”);
}

The first lines compose the XML simple structure. We first create the elements and then link them to create the hierarcy.

1. create an object of type XML
2. create the first element (“root”)
3. create another element (“value”) – it will become the child of “root”
4. create a text node from with the contents from the TextInput
5. make the text node the child of the “value” element
6. make “value” the child of the root.
7. append “root” as a child of the XML

The next two lines of code:
8. set the type of the content that will be POSTed to the php script
9. invoque the script.

Observations:

Please use POST. I had troubles using GET.

Display

The display part is simple too. You just have to load the .xml file that the php script has created on the site, parse it, and extract the value needed.

The Display flash has a simple TextBox which will be filled with the value from the xml on the load event (can be other events too).

The code is below. An XML object needs to be created, you have to give it the address from where to load the .xml as well as give it a function which will be called when the loading is complete (or when the loading of the .xml resulted in error). If the loading is successfull, then you take the value from the third child (first child is “root”, second child is “value” and third value is the node in which we are interested).

on(load)
{
var myxml:XML = new XML();
myxml.onLoad(true);
myxml.onLoad = handleLoad;
myxml.load(“
http://hex.ro/files/flashxml/value.xml“);

function handleLoad(success:Boolean)
{
if (success)
{
_root.textbox.text = myxml.firstChild.firstChild.firstChild;
}
else
{
_root.textbox.text = “error loading.”;
}
}
}

Source Code

Download the scripts here: Display.fla, EnterValue.fla, save.php (copy paste the text to a save.php file you create locally)