This tutorial is about how to load some content from a XML file in a flash project. It will be loaded a random text from the XML file.
STEPS TO FOLLOW:
1. First create a XML file, save it as xmlFlash.xml and put in the following lines:
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<item>
<title>Dracula</title>
<content>Count Dracula is the main character from the Bram Stoker's novel "Dracula". This novel was inspired from the historical Vlad Tepes (also known as Dracula).</content>
</item>
<item>
<title>Bucharest</title>
<content>Bucharest is political capital of Romania and also the main commercial city in the country. Bucharest was established capital of Rmanian Land in 1459 by Vlad Tepes (known as Dracula). </content>
</item>
<item>
<title>The Romanian Land</title>
<content>The Romanian Land is a historical region of Romania country. The Romanian Land was ruled by Vlad Tepes (known as Dracula) in three reings: 1448, 1456 - 1462 and 1476.</content>
</item>
</root>
2. Create a new flash document having dimensions about 400×300 pixels. Save it as xmlFlash.fla in the same folder with xmlFlash.xml. Insert one new layer and rename them from top to bottom: actions, content.
3. In the content layer put two dynamic text fields: select Text Tool (T), go to the Properties Panel and choose Dynamic Text as Text Type and the other properties you want (font, size, color, aligment etc.), then draw a text field on the stage. For the first text field give it an instance name of title_txt and content_txt for the second text field.
I also drawn a red line under the title_txt.
4.Now go to the actions layer, open the Actions panel (F9) and put the next AcionScript code. The ActionScript is commented to explain the steps how XML is loading in Flash:
var xmlFlash_xml:XML = new XML(); // create a new XML object
xmlFlash_xml.ignoreWhite = true; //
xmlFlash_xml.onLoad=function(success){ //onLoad event is triggered when the XML document hasbeen entirely loaded
if(success){ // success is true if the XMl has been loaded with no errors, and false else
var l =this.firstChild.childNodes.length; // firstChild property represents the rootnode of the XML document, childNodes property returns an array containing the children nodes of the current node. So, l variable holds the number of children of the root node
var c=Math.floor(Math.random()*l); // c is a random number between 0 and l-1
title_txt.text=this.firstChild.childNodes[c].childNodes[0].childNodes[0].nodeValue; // we display in title_txt the content on the title node from the c child of the rootcontent_txt.text=this.firstChild.childNodes[c].childNodes[1].childNodes[0].nodeValue; // we display in content_txt the content on the content node from the c child of the root
}
}xmlFlash_xml.load("xmlFlash.xml"); // the load() method of XML class is using Http to go to the specified URL and bring the entire XML document fo Flash
5. Great now! Test your movie (Ctrl+Enter) and see that a random text from the XML document is display in your text fields. Refresh this webpage to observe that the text is chancing.
Hope this tutorial about flash and XML was helpful.


Entries (RSS)
December 20th, 2007 at 3:51 am
Lovely stuff. The script worked just dandy. I was wondering if there was a way to add one field after the other like a blog and insert images as well. Do you know if there’s a way to do this?
Cheers,
Stein.
December 20th, 2007 at 8:12 am
Hi stein!
For adding images you can use loadMovie method (see also this tutorial http://www.simpleflashblog.com/2007/12/03/loading-external-content-in-flash-with-loadmovie-method/) and the url of the images you can keep in the xml file.
February 28th, 2008 at 7:17 pm
Hi, I did exactly as the tutorial instructed and I get an error when I attempt to load the flash movie (tutorial including xmlFlash btw). Here’s the errror message I get: Syntax error. Syntax error.the load() method of XML class is using HTTp to go to the specifies URL nd bring the entire XML document fo Flash.
Any ideas?
Thanks
March 11th, 2008 at 12:32 pm
@hywel try putting the last line “}xmlFlash_xml.load(”xmlFlash.xml”);” on two seperate lines like…
—————————–
}
xmlFlash_xml.load(”xmlFlash.xml”);
—————————–
@Author
Very Cool Tutorial, it helped me out a great deal and I thank you, I will be using the outcome
on my flash website which should be up by this weekend!
March 21st, 2008 at 4:01 am
Hi Bloggerita
I just wanted to congratulate you for what do you do for flash community. I am a Romanian too, but I live in Spain for the moment, I found your site by searching for a little more complex dynamic content solutions. Anyway, I have tested your solution and worked great.
I would like to share flash tricks with you if possible. You now have my email address so please contact me if you agree.
Again, GREAT BLOG!
Best wishes,
Adrian
May 20th, 2008 at 2:24 am
Hi,
Great work - it works like a charm.
But I have a question - if I would like to include CSS into these two dynamic text fields, how do I do that??
I’ve tried to include a CSS href into my .xml file, but I know that’s not enough.
How do I extend the AS code with CSS ??
May 28th, 2008 at 12:10 am
very nice……..
i want the scroll bar now for that text field, how to do that
if there is so much content in the .txt file, how can i see that in the swf file,ofcourse i can see that byscrolling middle mouse..but i want sroll bar in the left or right side to drag down and see the content
plz tell how
September 11th, 2008 at 1:27 am
Lovelly