Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » as » Load XML in Flash

Load XML in Flash

This tutorial will teach you how to load XML data into Flash, and display a basic result.

View an Example of this article before you get started.

If you want to skip the overall setup here is the completed movie. Just click on frame 1 for the code.

Here is the code.

importXML = new XML();
importXML.ignoreWhite = true;
importXML.onLoad = function(success) {
	if(success) {
		var depth = 0;
		var nextY = 10;
		count = 0;
		var root = this.firstChild; // The root node
		var attr = root.attributes;
		for(var i = root.firstChild; i != null; i = i.nextSibling) {
			tempMC = attachMovie ("box", "box" + count, depth ++, 
			{_y : (nextY) , _x : (105)} );
			nextY += tempMC._height + 2;
			tempMC.name_txt.text = i.attributes.name;
			count ++;		
		}
	} 
}
importXML.load("importdata.xml");

That is the actionscript we will use, now lets set up the movie.

Create a new flash movie, Create a new movie clip (Insert > New Symbol) draw a rectangle on the stage.

Make a new layer, name it 'txt', and add a dynamic textbox to the stage. set the instance name to 'name_txt'

Open the library, right click on the clip and check the Export option for Actionscript.

Now you are ready to start coding, click on frame 1, and either type the above code or copy & paste it into the code window. Ok lets start explaining the code.

importXML = new XML();
importXML.ignoreWhite = true;

The first line is declaring a new class. The name we use is "importXML". The next line is telling the XML parser to ignore any whitespace in the XML file.

importXML.onLoad = function(success) {

this is a function that will load the xml file, with a result of success True/False.

if(success) {

What is being done here is confirming the xml was loaded and if it was continue with the code.

var depth = 0;
var nextY = 10;
count = 0;
var root = this.firstChild; // The root node
var attr = root.attributes;

Now we will define a few variables to use later on. 'root' is the first piece of the xml file. In this example 'root' = select. 'attr' is the attributes found after the first node or parent node.

for(var i = root.firstChild; i != null; i = i.nextSibling) {
	tempMC = attachMovie ("box", "box" + count, depth ++, 
	{_y : (nextY) , _x : (105)} );
	nextY += tempMC._height + 2;
	tempMC.name_txt.text = i.attributes.name;
	count ++;		
}

for loop that will run through all the nodes in the xml file. "i != null" is saying keep running this loop until there are no more nodes. "i = i.nextSibling" is saying to read the next node in the xml. The attachMovie is taking X,Y parameters. The difference is the Y is set by the height of the previous clip. The next line is to display the text in the movie clip. This sends the "name" attribute from the xml into the "name_txt" textbox we created earlier. Final part in the for loop is to increment the count variable.

	} 
}
importXML.load("importdata.xml");

This is the part where Actionscript actually loads the xml file, and calls the onLoad function.

test the results. "Ctrl + Enter" If it didn't work you can post a question in the feedback form or you can contact me.

That is the end this weeks tutorial. I would love to see what amazing things you use this for.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Load XML in Flash

 Christopher Smith  Wed May 31, 2006 10:33 pm  
Here's something that some might find useful. Maxomatic at proto.layer51.com made an wonderful class for converting XML into an Array of Objects easily. There is an AS 2.0 version of this somewhere but I forget where. The AS 1.0 version is here: http://proto.layer51.com/d.aspx?f=1311
 Matt  Wed Oct 18, 2006 3:05 am  
Hi folks! well done on your XML coding!
I got question for you? In addition to the above code, I was wondering you can pull xml info from a list that includes 3 objects:

image/ name/ bio

What I am trying to do is make a list of Music Artists pop up in Swishmax (using dynamic text) and when the user clicks on any name, the xml info is picked up and displays the selection.

is this possible guys?

my email is: matthewbalzan@hotmail.com

much appreciated
Matt :-)
 hai to all  Thu Feb 8, 2007 2:48 am  
thanks for toturial

i had loaded the contents from the xml. but before loading i am getting the error in text box like _level10.desc_text... like that and displying the contents in text box. i want that _level10.desc_text... should not be displayed. i know it is taking time to load and to display but until that how to hide the text box?
 flyingace  Tue Mar 13, 2007 8:59 am  
I believe that the AS 2 version of the XML-Array class mentioned above can be found here:

http://proto.layer51.com/d.aspx?f=1340#c2521
 kefsan  Sun Jul 8, 2007 3:01 pm  
i am iranian.

content of site is very good.

thank you.

good luck.
 dhouse  Fri Jul 27, 2007 4:18 am  
I don't know what is happening with my script. I think my xml may be written wrong. But when I run this code and do a trace on i at the begininng the out put is the entire xml page as one long string.
Any clue?

Thanks
 sheetal bhatia  Tue Jul 31, 2007 2:43 am  
can you please tell me how i can make several text boxes scroll at a same time with a single scroller.

please help i am very much confused.

Regards,

Sheetal Bhatia
 sheetal bhatia  Tue Jul 31, 2007 2:44 am  
can you please tell me how i can make several text boxes scroll at a same time with a single scroller.

please help i am very much confused.

Regards,

Sheetal Bhatia
 sheetal bhatia  Tue Jul 31, 2007 2:44 am  
can you please tell me how i can make several text boxes scroll at a same time with a single scroller.

please help i am very much confused.

Regards,

Sheetal Bhatia
 sheetal bhatia  Tue Jul 31, 2007 2:45 am  
can you please tell me how i can make several text boxes scroll at a same time with a single scroller.

please help i am very much confused.

Regards,

Sheetal Bhatia
 Ranjith  Tue Sep 30, 2008 1:31 am  
Hi Friends,

Can you please guide how to load the XML into an Flash CS3 accordian.

Thanks in Advance
Ranjith Kumar
 Village idot  Fri Oct 17, 2008 3:35 pm  
I am having head banging trouble with calling my xml playlist to my JW player. I keep getting an Error stating that the xml file is not recognized...something or another. What steps should I take.

I do not know if it is my server configurations, or the file directory, or the file itself
 mkeefe  Fri Oct 17, 2008 7:31 pm  
@Village idot - What error (in Flash) are you getting? That will help me debug the problem for you.
 Banarjee  Wed Dec 3, 2008 1:59 am  
hi

I just copy and past the code which is there above i need to hide the dynamic text field when the other menu is clicked in the same stage can u guide me in this.

Regards
Banarjee.Rajan
 mkeefe  Wed Dec 3, 2008 5:24 am  
@Banarjee - If you need to hide a movieclip you would simply set the visible property to false or the _alpha property to 0 (zero).

Hope that helps.
 vp  Thu Jan 22, 2009 2:17 am  
sfg
 Carlos Lages  Wed Oct 28, 2009 5:43 pm  
You saved my job.
Thanks a lot!
 shwet  Mon Dec 14, 2009 6:44 am  
Hi,
Thanks, this work fine but when i am using this on a document width 996,it's not proper positioning.Pls help me to position it to top,left.


Thanking you
shwet
 Daren  Tue Dec 15, 2009 7:54 pm  
Hello,

Nice content. Keep adding more things into it. Good luck
 Download  Tue Feb 16, 2010 10:52 am  
You saved my job.
 lex frost  Fri Mar 12, 2010 7:29 am  
This works beautifully for me when previewing locally but as soon as i upload it doesn't load my xml feed. Any ideas anyone?

Many thanks

Lex
 mkeefe  Mon Mar 15, 2010 5:05 am  
@lex - Do you get any error messages or anything of that sort. Also check to make sure you have a crossdomain.xml file on the server you are requesting the XML feed from.
 Andrew  Mon Mar 22, 2010 2:37 pm  
Hi how can I load just one simple piece of text from a xml file, into a dynamic textbox place in the 4th frame, inside a movieclip in my scene 1?

THX
 Stella  Tue Mar 30, 2010 10:42 am  
I am trying to upload a short .xml data file into my flash cs4 movie in as3. I am completely new to as3 and can't understand what info needs to be replaced in order to connect to my flash movie. Can anyone give me what script is permanent and what changes according to each file? Thanks.
 jafaroddin  Thu May 27, 2010 10:18 pm  
i want to call an image with xml based in flash

Advace thanks..........
 David  Sat Nov 6, 2010 1:09 am  
I am using Flash 8 (Actionscript 2) and on one page I am uploading text from an external text file. This works fine when testing the swf file, however, when I load the swf file into an HTML page the external text will not load into the swf file. Does anyone have a clue as to what is happening and how I should correct this problem ??
Thanks.....David
 mkeefe  Tue Nov 9, 2010 4:51 am  
@Andrew - Look into the XML() class in ActionScript 3. You can also use E4X, if you prefer.

@Stella - Not sure if you are asking for an example or the complete script, please let me know.

@jafaroddin - Load the URL from the XML file and then use the URLLoader or Loader class to pull in the image.

@David - Make sure the remote server has a crossdomain file.
 Vikrant Aggarwal  Wed Nov 17, 2010 1:35 am  
this is my xml file.....

<data>
<student>
<name>Name : Vikrant Aggarwal</name>
<date>DOB : August 25 1982</date>
<sex>Sex : Male</sex>
<city>City : Dehradun</city>
<image>
<imagePath>an1.jpeg</imagePath>
</image>
</student>
</data>

this is my AS.......

function loadXMLData(loaded) {
if (loaded) {

_root.abc = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
abc_txt.text = _root.abc;

_root.abcd = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
abcd_txt.text = _root.abcd;

_root.abcde = this.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue;
abcde_txt.text = _root.abcde;

_root.abcdef = this.firstChild.childNodes[0].childNodes[3].firstChild.nodeValue;
abcdef_txt.text = _root.abcdef;

var path:String = this.firstChild.childNodes[0].childNodes[4].firstChild.nodeValue, imageLoader;
trace(path);
loadMovie(path, imageLoader);
}
else {
}
}
xmlFile = new XML();
xmlFile.ignoreWhite = true;
xmlFile.onLoad = loadXMLData;
xmlFile.load("data.xml");

i want to load an1.jpeg image with my imageLoader (mc)....i put all the files in same folder...
anyone help me out plz.........
 Rober  Tue Jan 10, 2012 1:44 am  
Here is something that many of you will find useful; I found this website www.flashxml.net and it has many free flash components that you can use on your website. You can change settings, images or skins trough XML. They also have many tutorials and the support team will help you in everything you need. Hope it was worth to post this here.
 Soumya Vinod  Tue Jan 17, 2012 1:58 am  
Found another relatively simpler way to do the same,posted at http://bbso.wordpress.com/2012/01/16/loading-xml-in-flash-as3
 John  Tue Jan 17, 2012 7:24 am  
Rober you saved my day. That is exactly what I was looking for. I searched for flash and xml because these components are very easy to customize. On flashxml.net you can find nice products, I recommend it, too.
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles