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. By Forrest posted on August 21, 2008 at 4:20 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    What if you wanted to disply two images (or more) on the same page (at the same time)?
    How is the AS2 code amended?
    Do you need to create a new MC?
    Does the 2nd MC need a different instance name?

  7. By swap posted on January 23, 2009 at 3:02 am
    Want an avatar? Get a gravatar! • You can link to this comment

    here ur given only with one image but i want to 3 to 4 images then how i have to give.

  8. By swap posted on January 23, 2009 at 3:06 am
    Want an avatar? Get a gravatar! • You can link to this comment

    i want to display on same MC with 3 to 4 images like rotating.

  9. By geon posted on September 24, 2009 at 2:43 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    it works,stop being greedy and go to barnes and nobles and figure the rest ungreatful bastards, he doesn’t owe you cuz you didn’t pay.

  10. Trackback