Main Content

You are here:

XML and Flash - AS2

Welcome to this Flash tutorial, a brief introduction to XML in Flash. This tutorial will cover the basics, and try to explain them fully so that you have a strong foundation on which to build in order to continue your XML learning. Let's begin by just making sure you understand the concept of XML. XML is an abbreviation for eXtensible Markup Language. It is similar to HTML by means of its structure, built around tags and attributes, but differs as it isn't really a programming language (more a formatting language), and doesn't have to have many strict rules (unless you set them). For example, this would be a perfectly fine XML document:

                                
                                
                                Hi
                                
                                
                                This is an XML document!
                                
                                
                                

This would not be a valid HTML document ("text" isn't the name of a tag), but it work in XML, as any tags (or "nodes" as they are known in XML) are accepted. There is obviously a lot more to discuss about XML, but that was just a quick recap, because this tutorial this isn't an XML tutorial, it's a Flash tutorial. Anyway, that's all you need to know to continue with this tutorial, so let's carry on.

How do I load XML into Flash?

That's simple, Flash already has an XML class, so just write:

var cats_xml = new XML();
                                cats_xml.load("cats.xml");

Here we create a new XML object and load into it (using the load function), a file called cats.xml. But what can we do? Well, the ideal time to do something with the file is once it's loaded, so change your code to:

var cats_xml = new XML();
cats_xml.onLoad = function(loaded){
        if (loaded){
                trace(this);
        }
}
cats_xml.load("cats.xml");

This will trace our cats XML file, so would trace something like this:

                                
                                
                                CATS!
                                
                                
                                Meow!
                                
                                
                                Meow!
                                
                                
                                Meow!
                                
                                
                                Meow!
                                
                                
                                Meow!
                                
                                
                                

But wait, we want the info in the tags, not just the whole XML document! So how do you get it out? Well, you need to learn that Flash stores XML data in a series of arrays and objects. Let's look:

                                var parent = new Object();
                                parent.childNodes = new Array();
                                parent.nodeName = "parent";
                                parent.childNodes[0] = new Object();
                                parent.childNodes[0].nodeName = "title";
                                parent.childNodes[0].childNodes = new Array();
                                parent.childNodes[0].childNodes[0] = new Object();
                                parent.childNodes[0].childNodes[0].nodeValue = "CATS!";
                                parent.childNodes[1] = new Object();
                                parent.childNodes[1].nodeName = "cat";
                                parent.childNodes[1].childNodes = new Array();
                                parent.childNodes[1].childNodes[0] = new Object();
                                parent.childNodes[1].childNodes[0].nodeValue = "Meow!";
                                parent.childNodes[2] = new Object();
                                parent.childNodes[2].nodeName = "cat";
                                parent.childNodes[2].childNodes = new Array();
                                parent.childNodes[2].childNodes[0] = new Object();
                                parent.childNodes[2].childNodes[0].nodeValue = "Meow!";
                                parent.childNodes[3] = new Object();
                                parent.childNodes[3].nodeName = "cat";
                                parent.childNodes[3].childNodes = new Array();
                                parent.childNodes[3].childNodes[0] = new Object();
                                parent.childNodes[3].childNodes[0].nodeValue = "Meow!";
                                parent.childNodes[4] = new Object();
                                parent.childNodes[4].nodeName = "cat";
                                parent.childNodes[4].childNodes = new Array();
                                parent.childNodes[4].childNodes[0] = new Object();
                                parent.childNodes[4].childNodes[0].nodeValue = "Meow!";
                                parent.childNodes[4] = new Object();
                                parent.childNodes[5].nodeName = "cat";
                                parent.childNodes[5].childNodes = new Array();
                                parent.childNodes[5].childNodes[0] = new Object();
                                parent.childNodes[5].childNodes[0].nodeValue = "Meow!";
                                

Uh-oh! So, what if we want to find just the title, then the first cat? Well:

var cats_xml = new XML();
                                cats_xml.onLoad = function(loaded){
        if (loaded){
                trace(cats_xml.firstChild.firstChild.nodeValue);
                trace(cats_xml.firstChild.firstChild.nextSibling.nodeValue);
        }
                                }
                                cats_xml.load("cats.xml");

So, the first child of cats_xml (cats) is accessed, and then the first child of that (title) is accessed. Then it traces the value. Finally, it traces the value of the next sibling of cats (cat).

This traces:

                                CATS!
                                Meow!
                                

So by now, you should understand the basics of using XML in AS2. Thanks for reading this tutorial,

Leon