Flash - XML Tutorial Actionscript 2.0 - The Basics


This is the Flash MX2004 version of the xml tutorial. There is also a MX version, which uses Actionscript 1.0. If you have been reading that already or you are familiar with xml you can go directly to the next page. In order to understand this tutorial you need to have a basic knowledge of OOP. You can either read the tutorial I wrote or any other introductory tutorial. I have listed some other sites there.

XML, which stands for eXtensible Markup Language, has gained of enormous importance in the last couple years. The main reason is that it allows to package a large amount of data in a rather condensed form, which can be recognized by computers. As an example go to this site, which is the database for medical journals and type XML into the search textbox. You will get about over 200 citations of Bioinformatics papers, where the use of XML is included. The medical journal database itself is built on XML. But I don't want to go further into the importance of XML because that is not the goal of this particular tutorial. Although I will briefly introduce the structure of XML, since it is required for this tutorial, you will find much better information about XML at other sites such as www.w3.org for example. An excellent tutorial for beginners to learn about XML is the site by the Webmonkeys. Tupps has a site and a lot of its content is dedicated to XML, in particular there is a FAQ section, which you should consult for more details. There are also some nice XML Flash applications and tutorials.

It is surprising that despite the capacity to use XML in Flash there seems to be a strong hesitation to get into the use of XML in Flash. Just look at some XML - Flash forums, which are not very populated except often for some who try to get into XML/Flash or some well experienced experts. Another problem are tutorials, which I found are much too difficult for beginners and keep those who want to try away. The same is true for books. However, once you start using XML/Flash you will find that it is not only a lot of fun but extremely useful and important.

But that is enough introduction. Let's get to work now. When you are here to study this tutorial, you have probably learnt already something about XML. XML is very similar to HTML except for two reasons:

           1. XML like HTML is using tags, but they are not recognized by browsers. We create our own tags, which make some sense and describe what we are dealing with. We have to write some code for browsers to recognize the tags.

           2. XML is a much stricter language than HTML. We always have to use an opening tag and a closing tag. We cannot mix capital and small letters and the closing tag has to be like the opening tag except for a slash. The order of tags is also important. Actually that is not bad, because an XML file is much more well structured than many sloppy HTML files.

If you really have never seen an XML file, let's have a look at a simple file, which we will also use as our first file using Flash.

                    <?xml version="1.0"?>//xml declaration
                    <!DOCTYPE items [//Document Type Definition or DTD
                       < !ELEMENT text (#PCDATA)>
                    <items>//root element
                        <text>This is XML.</text>//childnode (xml element) with textnode

Before we go into the details let's see how the browser interprets this file. Ok, the browser shows the file and that means there are no errors. In case of errors the browser would tell us. Now let's go into the details. Every XML file has a XML declaration. Next follows the Document Type Definition or DTD, which specifies each element of an XML document. In this case the elements of the rootnode items are only one text and it contains text and no further elements. For parsing XML in Flash we do not need a DTD. Since, however, XML files usually have a DTD, I included it here. If you want your XML files be available for other applications you have to add a DTD. After the DTD follows the actual contents of the XML file. We want to get aquainted here already with syntax used in Flash. All nodes within an XML file are referred to as childnodes in Flash. items would be the first child, text the first child of the first child and This is XML would be the first child of the first child of the first child. And there we are to access the xml file and its content.

This means that now it is time to use XML in Flash.