Flash/XML Syntax in Actionscript 2.0

In order to parse an XML file we need to know something about the syntax used to approach each element. First of all we need to upload our XML file. Even small XML files (over 400-500 bytes) take time to be parsed, since we use a button to load and parse the XML file. Therefore, we use a preloader, which is described in more detail on the last page of the tutorial. I included the preloader component in the tutorial package. This will cache the XML file at start.

When you read this tutorial you should have a basic understanding of the new AS2 syntax, which is used in MX2004. In order to use classes we have to create external scripts ending with the .as extension. If you have MX2004 PRO there is an AS editor and for the regular version you need to use a text editor. First we have to declare the class and the class path. In our case we have all the external scripts in a folder called ascripts. Therefore, we need to include the folder name as part of the path. The name for the class is showXML and now we have to define all methods and properties. First we define the major variables used. We make them private, because they should be available only for this class. Now it is important that we give the correct var type to each var, which will determine its properties. In the flash editor type var myXML: and you will see that a window pops up with all possible var types. myXML is the instance name for a new xml object and therefore, myXML is of type XML. If you would define a different type, there will be an error message later that the type does not match.


          class ascripts.showXML {
                private var myXML:XML;
                private var xmlFile:String;
                private var startItems:Function;
                private var mainField:TextField;
                private var textField1:TextField;
                private var textField2:TextField;
                private var textField3:TextField;
                

Next we define the var xmlFile, which holds the name of the xml file and therefore is of type String. When we load the xml file we create an instance of a function to parse the file startItems. This is a var for a Function. Then we have a number of textfields in our movie and yes each textfield is named by a var of type TextField. We can also give it the type String, since the textfield will in the end be pieces of strings. Now it is time to define the constructor for our class, which is the function showXML{}. Throughout this tutorial the constructor is empty and we deine everything in a new function.


                public function showXML(){
                }
                
                public function sXML(xmlFile){
                      _root.mainField.html = true;
                      myXML = new XML();
                      myXML.load(xmlFile);
                      myXML.ignoreWhite = true; 
                      myXML.onLoad = startItems;    
                      function startItems(success:Boolean){
                            if(success){
                                  listItems(this.firstChild);
                            } else {
                                  _root.mainField.text="No file loaded!";
                            }
                       }
                       

The constructor function is always public to be accessible. We do not have to add the word public, because by default functions are public, when not declared private, but we do it to maintain clean syntax. At this point we set our main textfield html to true, since we will need it a bit later. We are not allowed to do this within the class definition, only within functions. We could also define the other textfields here, however I like to define var within a particular method when they are needed unless they are needed in more than one method.

Next we create an instance of the xml object, which is held by our var myXML. We then eliminate white space, which we have created in our xml file and which is also recognized by the parser as nodes. Then we trigger the onLoad event, which will tell us if the xml file was successfully received. We test this with the function startItems, which has the argument success. The type of arguments var can be defined directly when they are called the first time. We need not add var, since the player obviously recognizes arguments as var by default. success is a Boolean var and can therefore be either true or false.

Now finally we can initiate the parsing of our xml file using the function listItems with the argument this.firstChild. This covers the whole content of the xml file.


function listItems(firstItem:XMLNode){
      _root.textField1.html = true;
      _root.textField2.html = true;
      _root.textField3.html = true;
      var fItem:XMLNode = firstItem.firstChild;
      _root.mainField.text = fItem.firstChild.nodeValue;
      _root.textField1.text = "node name: "+firstItem.nodeName+","+"   
      type: "+firstItem.nodeType +","+"   value: "+firstItem.nodeValue;
      _root.textField2.text="node name: "+fItem.nodeName+","+"   
      type: "+fItem.nodeType +","+"   value: "+fItem.nodeValue;
      _root.textField3.text="node name: "+fItem.firstChild.nodeName+","+"   
      type: "+fItem.firstChild.nodeType +","+"   
       value: "+fItem.firstChild.nodeValue;
      }
   }
}
            

We are now ready to parse the file using the function listItems. This function has an argument firstItem and as you have seen above the content of firstItem is this.firstChild, which is nothing else than a XMLNode and therefore the argument firstItem is of type XMLNode and its content can be passed on. Here now we set the properties of the other textfields, since we will need them. We also create another var of type XMLNode fItem just for convenience.

The last thing we need to know is how we call the external script within the flash movie. As a convention the best is to import the script by using the whole path. We then need to only use the class without the path name when we create an instance of the class as shown below.


           import ascripts.showXML;
	       var newxml:showXML = new showXML();
	       button1.onRelease = function() {
	           newxml.sXML("xml_tutorial_1.xml");
	       };

          

And now in the following movie we will learn about the structure and the way flash parses xml. So let's have a look at the swf.

Let's analyze the individual text parts. In the mainfield the main statement we want to show is presented This is XML. We have two node names items and text. They have a value of 0, because they are names. Node names are of type 1. This can be important if we want to do something with them, then we just use the node type. Later you will see an application. Our last node does not have any name and is therefore of type 3 but it has a value. These nodes are called text nodes. In fact the value is the main statement of our xml file This is XML.

We learnt a lot so far and have an initial understanding of how flash parses xml. So now in the next chapter we start building an application for xml, which will be a small database for models.


PREVIOUS          NEXT