<?xml version="1.0" encoding="iso-8859-1"?>
<tutorial>
   <description>Flash Tutorial: xml parsing with flash mx 2004</description>
   <keywords>flash tutorial, flash and xml, xml parsing with flash</keywords>
   <title>Flash and XML</title>
   <slug>
<b>Skill Level:</b>Advanced<br></br>
<b>Knowledge Needed:</b>Actionscript, buttons, dynamic textboxes<br></br>
<b>Number of Steps:</b> 7<br></br>
<b>The Movie</b><br></br>
-----------------------------------------------<br></br>
<flash><width>250</width><height>250</height><name>xml_parsing.swf</name></flash><br></br>
------------------------------------------------<br></br>
<link><url>xml_parsing.fla</url>Download Flash File</link><br></br>
Learn how to create a flash phonebook using the power of xml with flash.
   </slug>
   <step>
      <left></left>
	  <stepnumber>1</stepnumber>
	  <text>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 <b>stop();</b> action.
	  Next we will create the form.</text>
	</step>
	<step>
      <left><image><name>pict1.jpg</name><width>250</width><height>265</height><alt>Picture 1</alt></image></left>
	  <stepnumber>2</stepnumber>
	  <text>
	  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 <b>name_txt</b>, <b>email_txt</b>, <b>web_txt</b>, and <b>entryNum_txt</b>.  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 <b>entryNum_txt</b> textbox between these two buttons.  Now the hard part is over and we get to have 
	  some fun.</text>
   </step>
   <step>
      <left></left>
	  <stepnumber>3</stepnumber>
	  <text>
	  The first thing you are going to need is the xml file to load into your flash movie.  Right click this <link><url>contacts.xml</url>link</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.
	  </text>
   </step>
   <step>
      <left></left>
	  <stepnumber>4</stepnumber>
	  <text>
	  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
	  <b>stop();</b> action.
<![CDATA[ 
<pre>
var contacts = new XML();
contacts.ignoreWhite = true;
var entry = 0;
var total = 0;
var current = 0;
</pre>
]]> 
	First we are declaring a variable called <b>contacts</b> 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 <b>entry, total, and current</b> which will be used when we are
	loading the data.
	  </text>
   </step>
   <step>
      <left></left>
	  <stepnumber>5</stepnumber>
	  <text>
	  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 <b>stop();</b> action.
<green>
<![CDATA[ 
<br />
contacts.load("contacts.xml");<br />
contacts.onLoad = function(success) {<br />
if (success) {<br />
	&nbsp;&nbsp;&nbsp;name_txt = this.firstChild.childNodes[_root.entry].attributes.name;<br />
	&nbsp;&nbsp;&nbsp;email_txt = this.firstChild.childNodes[_root.entry].attributes.email;<br />
	&nbsp;&nbsp;&nbsp;web_txt = this.firstChild.childNodes[_root.entry].attributes.website;<br />
	&nbsp;&nbsp;&nbsp;_root.total = this.firstChild.childNodes.length;<br />
	&nbsp;&nbsp;&nbsp;_root.current = _root.entry + 1;<br />
	&nbsp;&nbsp;&nbsp;entryNum_txt = _root.current+" of "+ _root.total;<br />
}<br />
};<br /><br />
]]> 
</green>
	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.  <b>contacts</b> 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.  <b>success</b> 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.<br />
	The line <b>name_txt = this.firstChild.childNodes[_root.entry].attributes.name;</b> 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 <b>contacts</b> and under that we have a list of <b>entries</b>.  childNodes[0] is the first node, childNodes[1] is the 
	second node and so on.<br />
	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.
</text>
   </step>
   <step>
      <left></left>
	  <stepnumber>6</stepnumber>
	  <text>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.
<![CDATA[ 
<pre>
on(press) {
	if(_root.entry + 1 == _root.total) {
		_root.entry = 0;
	} else {
		_root.entry += 1;
	}
	_root.contacts.load("contacts.xml");
}
</pre>
]]> 
	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.
<![CDATA[ 
<pre>
on(press) {
if(_root.entry == 0) {
	_root.entry = _root.total - 1;
} else {
	_root.entry -= 1;
}
_root.contacts.load("contacts.xml");
}
</pre>
]]> 
	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.
	</text>
	</step>
   <step>
      <left></left>
	  <stepnumber>7</stepnumber>
	  <text>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.</text>
	</step>
</tutorial>
