type = 'application/x-shockwave-flash'
data = 'http://sitepointstatic.com/flash/xml.swf'
width = '400'
height = '400'
bgcolor = 'black'
>

Here, I’ll show you how to load xml data into Flash. Note that this tutorial assumes that you have a very good knowledge of xml. Download the sample files here.

1. Let’s start by creating a xml document. Open notepad and insert:

<?xml version="1.0" encoding="iso-8859-1"?> 
  <xmltest>
      <parentnode>
        <node1>First node value</node1>
       <node2>Second node value</node2>
       <node3>Third node value</node3>
       <node4>Fourth node value</node4>
       <node5>Fifth node value</node5>
      </parentnode>
  </xmltest>

2. Save the file as flash.xml.

3. Open a new movie in Flash. I have set the movie properties at width:370 and height:200.

4. Now, create dynamic text, and give it a variable name "txt". Be sure to check html in the text options pallete.

5. Right click on the first and only frame in the movie, and select Actions. Insert:

XML_var = new XML(); 
// now load up the url.
XML_var.load("http://www.flashcircle.com/swffiles/flash.xml");
// when xml is loaded call functon displayXML
XML_var.onLoad = displayXML;
// display in txt xml is loading
txt = "Loading XML data...";
function displayXML()  
{
mainTag = new XML;
elementTag = new XML;
articleList = new Array;
elementList = new Array;
mainTag = this.firstChild.nextSibling;
articleList = mainTag.childNodes;  
txt = "";
//loop through xml
for(i=0;i<=articleList.length;i++)
{//start for
elementList = articleList[i].childNodes;
//start for
for(j=0;j<=elementList.length;j++)  
{//start for
elementTag = elementList[j];
head = elementTag.firstChild.nodeValue;
if(elementTag.nodeName.toLowerCase() == "node1")
{txt += head +"";}
if(elementTag.nodeName.toLowerCase() == "node2")
{txt += head +"";}
if(elementTag.nodeName.toLowerCase() == "node3")
{txt += head +"";}
if(elementTag.nodeName.toLowerCase() == "node4")
{txt += head +"";}
if(elementTag.nodeName.toLowerCase() == "node5")
{txt += head +"";}
}//end for
}//end for
}

Let me explain how each line works.

XML_var = new XML()
...declare XML_var as a new xml object.

XML_var.load("http://www.flashcircle.com/swffiles/flash.xml")
...load the xml file.

XML_var.onLoad = displayXML
...if file is loaded, call the displayXML()/#epc#/ function.

for(i=0;i<=articleList.length;i++)
...iterate using for loop through xml file to display the elements.

Georgina has more than fifteen years' experience writing and editing for web, print and voice. With a background in marketing and a passion for words, the time Georgina spent with companies like Sausage Software and sitepoint.com cemented her lasting interest in the media, persuasion, and communications culture.

Get your free chapter of Level Up Your Web Apps with Go

Get a free chapter of Level Up Your Web Apps with Go, plus updates and exclusive offers from SitePoint.


No Reader comments

Related books & courses
Available now on SitePoint Premium

Preview for $1