Stylesheets, Flash and XML for Dummies
This tutorial will show you how to include an external XML text file into Flash using external Cascading Style Sheet (CSS) to get crisp, clean and elegantly styled text within your text boxes. The latter serves two primary advantages over any other approach… (1) XML allows you to populate Flash content-driven applications with data from a simple text file. This is perfect for designing reusable application where’s nobody has to come into the Flash file again; all they have to do is add new entries to the XML file. (2) Cascading Style Sheets can be used to group style rules, which can be applied to XML elements allowing you to format all of your text boxes quickly and easily by editing just one single text file.

So before you go bold, refer back to this article and try some of this code. To write my XML, I generally use Notepad++, which it’s just a nicer version of the typical Notepad that comes with Windows; but you can use any text editor doesn’t have to be this one. Here’s the XML file we start with.
<mynursery_rhymes>
<itsybitsy>The Itsy Bitsy Spider</itsybitsy>
<foofoo>Little Bunny Foo Foo</foofoo>
<littlelamb>Mary had a little lamb</littlelamb>
</mynursery_rhymes>
It looks like plain old HTML doesn’t it? Absolutely, XML is very similar in many ways to traditional HTML - the difference being is that you are no longer restricted by a predefined list of tags; you can actually makeup the tag names yourself. Nevertheless don’t get too carried away, there are certain rules to naming your tags, such as not including spaces in the names, keeping your names short, simple, descriptive and all lower case. And always remembering to close your tags, every opening tag must have a matching closing tag.
The above XML reads as follow - the first line in any XML file is this XML declaration, HTML also has something like this and it’s basically just saying that this is an XML document and it should be treated as such. This is generally required but technically Flash will work either way. The next line is the root element or parent node, node is just another word for element, again this is very similar to the html tag in HTML in that every other tag will have to be nested between these two tags.
I will explain about the stylesheet in a minute, for now lets save this as “rhymes.xml” and head on over to Flash and create the code and visual elements needed to read our XML document. Open a new movie (.fla) - add two layers an Actions layers and a Visuals layer. Highlight the first frame in the Visuals layer then selects the text tool in the tools bar and drag it along the stage to create your text filed. Finally, in the text Property inspector (Ctrl F3 to access), resize it to about 400*350 pixels and set the follow options:
1. Select “Dynamic Text” from the Text Type drop-down menu. 2. In the Instance Name text box of the Property inspector, give it an instance name of “content_txt”. 3. For Line Type, select “Multiline” to ensure the text wraps correctly. 4. Select “Render text as HTML” and “Selectable” from the Properties inspector

With that out of the way, select the first frame on your Actions layer and opening the Actions panel (F9 to access) paste this code.
var format = new TextField.StyleSheet();
var path = "crisp_styles.css";
format.load(path);
content_txt.styleSheet = format;
// Load XML source
xmlData = new XML();
// ignore any white spaces in the XML content
xmlData.ignoreWhite = true;
// load our XML file into our XMl object
xmlData.load("rhymes.xml");
// check that the XML file has been loaded successfully
xmlData.onLoad = loadXML;
What the hell? Right lets go over this. What we’ve done is, create an instance of the StyleSheet class, load our stylesheet using the ‘load’ method and then associate it with our text field. Also, load our XML into Flash using the XML object and call our loadXML function to determine if our XML file loaded successfully. It seems now is a good time, or as good time as any to write our loadXML function so, open the Actions panel again and enter the following.
if (loaded) {
content_txt.condenseWhite = true;
content_txt.htmlText = xmlData;
} else {
content_txt.text = "Error while loading XML document";
}
}
That’s it! Make sure to save your .fla file in the same folder as the rhymes.xml file and press Ctrl+Enter to test your movie, if you’ve followed the instructions correctly you should see some thing like this.

By George, I think we’ve got it! Though the text still seems a little dull and out of focus. Lets cut to the chase, shall we? I’m hoping you know something about stylesheets already, hopefully from working with HTML and stylesheets (CSS). If not, it’s no biggie. CSS in ActionScript is somewhat limited. The properties listed bellow are all you have access to.
display;
font-family;
font-size;
margin-left;
margin-right;
text-align;
text-decoration;
To make this a bit easier for you. I’ve already created the stylesheet for you. Feel free to use it or create your own.
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #689891;
display: inline;
}
foofoo {
font-family:arial, lucida console, sans-serif;
font-size: 11px;
color: #C1C6E3;
display: inline;
}
littlelamb {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
color: #000000;
display: inline;
}
Save the stylesheet as “crisp_styles.css” in the same folder as your other files and, switch back to Flash to display the movie.

Not bad! But, how about adding a little more text between our XML tags to see what a real paragraph might look like. Heck, lets also add an image while we’re at it and amend the stylesheet by adding a class to give emphasis to special words within the paragraph then, we’ll return to the XML document and apply our new class to any word withing our tags as follows.
(1) Open the XML file and add this.
<mynursery_rhymes>
<itsybitsy><img align="left" src="rhymes.jpg" width="156" height="165" hspace="10" vspace="10"/>The Itsy Bitsy Spider
Climed up the water spout;
Down came the rain
And washed poor the Spider out</itsybitsy>
<foofoo>Little Bunny Foo Foo
hoppin’ through the forest,
scoopin’ up the field mice
and boppin’ em on the head.</foofoo>
<littlelamb>Mary had a little lamb,
little lamb, little lamb,
Mary had a little lamb, its fleece was white as snow.</littlelamb>
</mynursery_rhymes>
(2) Go to the stylesheet and paste this chunk of code.
color: #FCB6B8;
}
(3) Return to the XML to apply our new class.
<mynursery_rhymes>
<itsybitsy><img align="left" src="rhymes.jpg" width="156" height="165" hspace="10" vspace="10"/>The Itsy Bitsy Spider
Climed up the water spout;
Down came the rain
And <span class="note">washed</span> the Spider out</itsybitsy>
<foofoo>Little Bunny Foo Foo
hoppin’ through the forest,
scoopin’ up the field mice
and <span class="note">boppin</span>‘ em on the head.</foofoo>
<littlelamb>Mary had a little lamb,
little lamb, little lamb,
Mary had a little lamb, its fleece was <span class="note">white</span> as snow.</littlelamb>
</mynursery_rhymes>
Needless to say you’ll need to make your own image, also I should point out that the Flash Player does not support progressive JPEG files. Now, if all is well the Flash should look like this.

That’s basically it-it might seem like a lot at first but it really isn’t all that difficult. If you still find this hard to understand, leave a reply.


{ 12 comments… read them below or add one }
This is nice! What if I have 3 or 4 separate dynamic boxes on the page. Lets say a title on the top right. body on the left, and a dynamic box on the bottom. How would I populate them using the xml and css or is it possible. Thanks
One way to do it would be to access each element individually, so rather than dumping everything together as we did in the example.. content_txt.htmlText = xmlData; but rather we would access each element individually like this…
content_txt.htmlText = this.firstChild.childNodes[0];
Remember a node is just another word for element, so the firstChild is the root element, and the others are its little children…The above would then display only the fist element after the _root element which in this case it’s called itsybitsy… like this.
—————————————————
The Itsy Bitsy Spider Climed up the water spout…
—————————————————
Then you could create as many text fields as you want and assign their individual values… for example
title_txt.htmlText = this.firstChild.childNodes[0];
main_txt.htmlText = this.firstChild.childNodes[1];
footer_txt.htmlText = this.firstChild.childNodes[2];
the above would display individually in each of the text fields…
Thank you for the code. A weird thing has happened. I published the site at this address and it worked fine.
artemariana.com.br/dfc (>>>program >>>about)
on the other hand, when I changed the server to the final address - designfc.com - it uploads only the text file, the stylesheet has not been applyied….Both servers have the same files…..I transfered the files from one to another.
I can not figure out what is happening….
Thank you
Cool site… Take a look at this line in your fla
var path = “crisp_styles.css”;
Then compare that to where the .css file is actually residing. for example
var path = “crisp_styles.css”
is telling Flash to look for the style sheet in your root directory as follows
designfc.com/crisp_styles.css
The question is. is that where the style sheet really is, or is it somewhere else like
designfc.com/css/crisp_styles.css
In which case you should change your code to this
var path = “./css/crisp_styles.css”; or this
var path = “http://designfc.com/css/crisp_styles.css”;
Hi Collado,
Tks a lot…After I saw you message I tried to point the css file to the former server (since this one was working) http://www.artemariana.com.br/dfc/about2….
unfortunately is not working either…..on the www.designfc.com
…I can not figure out…..
www.artemariana.com.br/dfc
www.designfc.com
both servers have the same files.
Annoying…..thank you for your attention…Mariana
Hi Mariana,
without actually looking at your code etc, I really cant say… I’m sorry… if you want us to install the css for you write to sales@foamers.net for a quote
Hi Collado!!!!
Finally I found the mistake……The first server was a apache and the second was ISS…..The first read the css without extension, the second didn\\\’t. And I found out a space was missing after a \\\”=\\\”…..I fix both and worked….Thank you a lot for your attention…
www.designfc.com
regards,
Mariana
Thank you so much for the tutorial. It was informative. I would like to see how the StyleSheets work with the DataGrid component in flash.
Sunny
Collado,
I would like to know more details about Will’s comment (from 1.24.08) about multiple dynamic text boxes. Seems like replacing “content_txt.htmlText = xmlData; ” isn’t enough. I imagine I would have to alter other instances of the call to “content_txt” elsewhere in the script. Any details on how to deal with “content_txt.styleSheet = format; “, “content_txt.condenseWhite = true;” and so on would be greatly appreciated.
Thanks
This is really cool! I’m trying apply styles to xml using Actionscript 3. Have you tried this yet? If so, I’d love to see an example!
The example above does not work.
I don’t understand - it works for everybody else… maybe what your mean to say is that “YOU” are unable to get it to work… that I can understand
Leave a Comment