Flash Script – Load XML Data into Flash

By Georgina Laidlaw

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"?> 
        <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>

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.
// 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
{//start for
elementList = articleList[i].childNodes;
//start for
{//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.

...load the xml file.

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

...iterate using for loop through xml file to display the elements.

Meet the author
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.

No Reader comments


Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in Front-end, once a week, for free.