Stylesheets, Flash and XML for Dummies

by Collado on January 21, 2008

stylesheets,flash and xmlThis 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.

Not long ago, editing text in Flash left little to be desired. Any formatting and updating of the text boxes had to be done within the FLA file, so a Flash designer would have to be called on for even the slightest modification – you would have to reopen the source file, go through the whole movie editing text boxes one at a time, recompile the new SWF file, then maybe, if blessed with the worlds nicest client you might get lucky and not have to redo it over and over again. Not to mention Flash’s anti-aliasing feature, which tends to hinder rather than serve any purpose at all. This became further complicated and even more of chore when in the process fonts would come out of focus (because of positioning). I’ve seen people try all sorts of ways to get rid of that blurriness and end up ripping their hair out, because all they get is what you see bellow.

blurry Flash's anti-aliasing

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.

< ?xml version="1.0" encoding="iso-8859-1"?>
<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

blurry Flash's anti-aliasing

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.

// load  my stylesheet
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.

function loadXML(loaded) {
        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.

blurry Flash's anti-aliasing

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.

color;
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.

itsybitsy {
        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.

blurry Flash's anti-aliasing

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.

< ?xml version="1.0" encoding="iso-8859-1"?>
<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.

.note{
       color: #FCB6B8;
}

(3) Return to the XML to apply our new class.

< ?xml version="1.0" encoding="iso-8859-1"?>
<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.

blurry Flash's anti-aliasing

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 }

1 will 01.24.08 at 5:46 am

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

2 Collado 01.24.08 at 7:33 am

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…

3 Mariana 02.19.08 at 3:41 pm

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

4 Collado 02.19.08 at 4:48 pm

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”;

5 Mariana 02.19.08 at 11:57 pm

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

6 Collado 02.20.08 at 8:10 am

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

7 Mariana 02.20.08 at 4:25 pm

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

8 Sunny Sharma 02.20.08 at 5:27 pm

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

9 brian 07.04.08 at 12:59 pm

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

10 susan 08.17.08 at 8:44 pm

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!

11 Mykal 10.07.08 at 7:59 pm

The example above does not work.

12 Collado 10.07.08 at 9:31 pm

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

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Customize Your Template -   Foamers provides a wide range of premium website templates together with professional template customization to fit your individual business needs.

Start by  -  choosing a template. Once your selection is made, contact us with some general information (e.g. Template ID, some suggested color schemes, colors to avoid, styles or websites that you likes etc).

Basic Package -  $380 -  Customizations: Adding your logo, changing all links, graphics, text, making sure that the contact form works and up to 5 whole pages of content.