
Flash and XML
Skill Level:Advanced
Knowledge Needed:Actionscript, buttons, dynamic textboxes
Number of Steps: 7
The Movie
-----------------------------------------------
------------------------------------------------
Download Flash File
Learn how to create a flash phonebook using the power of xml with flash.

First open up a new flash document. We are going to create the form elements and then get into loading the xml. (Please refer to the picture to see how everything
was arranged.) First of all you will need to make three layers. Name them actions, text, textBoxes. In the first frame of the actions layer add a
stop(); action.
Next we will create the form.


To create all of the form elements you will need four dynamic textboxes and two buttons (be sure to select dynamic and not static or input for the textboxes). Once you have made the textboxes and
lined them up accordingly
give them variable names of
name_txt,
email_txt,
web_txt, and
entryNum_txt. Next you should put some sort of static label for the name, email, and website textboxes to
show what those fields will be displaying. We will deal with the last textbox in a second. Once that is done you need to make the buttons to navigate through the contact list. I simply made a triangle
graphic, converted it to a button and then duplicated it for the back button. I also placed my
entryNum_txt textbox between these two buttons. Now the hard part is over and we get to have
some fun.

The first thing you are going to need is the xml file to load into your flash movie. Right click this
link and click "save target as". Be sure
to save it to the same directory that your flash movie will be saved in. If you are having trouble understanding the xml file, please check out akash's tutorial on xml basics, but
don't worry, all you need is that file and flash will do all of the work for you.

Finally we get to the fun part. We are going to be using flash's built in XML object to load the xml file and display the appropriate data. Add the following code into the actions layer above the
stop(); action.
var contacts = new XML();
contacts.ignoreWhite = true;
var entry = 0;
var total = 0;
var current = 0;
First we are declaring a variable called
contacts to handle all of the xml. The second line tells flash to ignore any and all extra whitespaces that may be in the xml file. If you are having
trouble loading data from xml right, not having this line could be the reason. The last thing we are doing is declaring 3 variables called
entry, total, and current which will be used when we are
loading the data.

Now we need to load the data and write the function that will handle loading and parsing the loaded data. Add the following code right under what you just wrote and before the
stop(); action.
contacts.load("contacts.xml");
contacts.onLoad = function(success) {
if (success) {
name_txt = this.firstChild.childNodes[_root.entry].attributes.name;
email_txt = this.firstChild.childNodes[_root.entry].attributes.email;
web_txt = this.firstChild.childNodes[_root.entry].attributes.website;
_root.total = this.firstChild.childNodes.length;
_root.current = _root.entry + 1;
entryNum_txt = _root.current+" of "+ _root.total;
}
};
There is kind of alot going on here, but we'll get through it. The first thing we are doing here is loading our xml file.
contacts is a variable being used to store the xml data loaded.
The next line starts the function which will handle all the parsing and loading of data into our form elements.
success is the name of our xml file being loaded via contacts.load and the top if
statement just verifies that the file is actually loaded.
The line
name_txt = this.firstChild.childNodes[_root.entry].attributes.name; tells
flash to look into the xml file just loaded (this) then move down to the first node or xml element (.firstChild) and then move down to the appropriate entry (.childNodes[_root.entry]) and finally to grab
the appropriate data (attributes.name). Our xml file contains a main element of
contacts and under that we have a list of
entries. childNodes[0] is the first node, childNodes[1] is the
second node and so on.
The next thing we are doing is telling flash how many total entries there are so we can see how many we have to browse through. Then we tell flash which entry we are currently looking at. We add one to
_root.entry because 0 is the first element in the node array, but we want it to show that we are looking at entry 1, not entry 0. Then we output which entry we are looking at out of how many.

Finally, we will add the code to the buttons so that we can browse through our new phone book. copy the following code on the button that will move you ahead in the phonebook.
on(press) {
if(_root.entry + 1 == _root.total) {
_root.entry = 0;
} else {
_root.entry += 1;
}
_root.contacts.load("contacts.xml");
}
Here we are checking to see if the current entry is the last one in our xml file. If so we cycle back to the beginning. Otherwise we just move ahead one and view the next entry.
Now add the following code to the button that will move back in the phone book.
on(press) {
if(_root.entry == 0) {
_root.entry = _root.total - 1;
} else {
_root.entry -= 1;
}
_root.contacts.load("contacts.xml");
}
In this code we are doing the same thing as above, but now we are checking to see if we are at the first entry. If so we go the last entry, otherwise we just move backwards one and view that entry.

Well, that's it. We are done. Hopefully this tutorial showed you some of what flash can do with xml and has let you think of ways to incorporate it in your projects. XML is a very powerful
language and flash's support of it makes a great combination. I encourage you to browse the flash help files to find out more about the xml class. If you have any questions be sure to drop
me an email.
Copyright © 2000-2010 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.
kdfj