Feature
Post

Category
Code

Pulling XML into Flash

When developers and designers start really utilizing the power of flash and the potential it can bring to applications and creative content, a question that frequently arises is how to load data in from a xml page and use it in Flash. This is a quick tutorial to show how easy the process is and once you have these basics the possibilities are endless.

First we will create a simple xml document called images.xml. Here is the code:

<images> <image image="img/img1.jpg" caption="Brian Self- Flash Programmer"/> </images>

We are only going to use one simple xml node to keep it simple. Save this file in a directory called xml.

Then create an FLA and call it “Main.” Put a dyncamic text field on the stage with an instance name of myText and be sure to embed the font, uppercase, lowercase and punctuation. Then draw a rectangle and hit F8 to make it a movieClip and give it an instance name of myImageHolder.

Then create an AS layer and paste in this code:

// create the XML variable var xml:XML = new XML(); // you must ignore whitespace xml.ignoreWhite = true; // the function that is called when the xml is loaded xml.onLoad = function() { // tells you the number of child nodes var nodes = this.firstChild.childNodes; // tells you how many items you have numOfItems = nodes.length; // attach icons for (var i = 0; i<numOfItems; i++) { // attach image to the myImageHolder MovieClip myImageHolder.loadMovie(nodes[i].attributes.image); // set the text myText.text = nodes[i].attributes.caption; } }; // load the xml xml.load("xml/images.xml");

If you run this you will see that the content is now populated from the xml page.

Simple as that! Remember, in coding and designing there is always an increasingly difficult way of doing things and a elequently simple way. Keeping it simple makes debugging a quick and painless process and always assume you will have to debug at some point in your designing life.

  1. By fille posted on February 12, 2008 at 1:47 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Thank you so much for your help ! I’ve spent 4 hours before manage to do what I want ;) cheers pal

  2. By Matthew posted on May 22, 2008 at 3:55 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    I am getting this error:

    Scene=Scene 1, Layer=Layer 2, Frame=1: Line 21: Syntax error.
    xml.load(“xml/images.xml”);

    What version of flash are you using? I am using Studio MX. Thanks!

  3. By Matthew posted on May 22, 2008 at 4:28 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    I loaded it up in CS3 and I am getting the same error

  4. By Robbie posted on June 3, 2008 at 2:20 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Mate! Champion! Was looking around for a simple one layer xml input and here I found it! Every other site I looked at went into too much detail for what I wanted! Should have come here 3 hours ago!

  5. By Jake posted on June 11, 2008 at 9:55 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Can you format the caption text?

  6. Trackback

    Your words are your own, so be nice and helpful if you can. If this is the first time you're posting a comment, it might go into moderation. Don't worry, it's not lost, so there's no need to repost it! We accept clean XHTML in comments, but don't overdo it please.