Flash Script – Load XML Data into Flash

By Georgina Laidlaw
We teamed up with SiteGround
To bring you up to 65% off web hosting, plus free access to the entire SitePoint Premium library (worth $99). Get SiteGround + SitePoint Premium Now


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.

The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
We teamed up with SiteGround
To bring you up to 65% off web hosting, plus free access to the entire SitePoint Premium library (worth $99). Get SiteGround + SitePoint Premium Now
Login or Create Account to Comment
Login Create Account