Macromedia Flash XML Tutorial
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.
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.
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.
The “Save” button has this code for the click event:
var myxml:XML = new XML();
root = myxml.createElement(“root”);
element = myxml.createElement(“value”);
textValue = myxml.createTextNode(this._parent.mytextbox.text);
myxml.contentType = “text/xml”;
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.
Please use POST. I had troubles using GET.
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).
var myxml:XML = new XML();
myxml.onLoad = handleLoad;
_root.textbox.text = myxml.firstChild.firstChild.firstChild;
_root.textbox.text = “error loading.”;