Download the files here.
TUTORIAL CONTENTS
  • Introduction
  • Flash/XML Syntax
  • Accessing nodes
  • Digging deeper
  • Power of Attributes
  • HTML and Text in XML
  • Making a Search Engine
  • Preloading XML files
  • XML/FLASH SITES AND TUTORIALS
  • Tupps web site
  • Flash 5 and XML
  • XML Table Constructor Kit
  • Using an XML file to load Chart components
  • Macromedia XML tutorial
  • Integrating XML and Flash in a Web Application
  • AUTHOR´s XML TUTORIALS/FILES
  • XML searchable database I
  • URLs and Asfunctions (II)
  • XML searchable database III
  • XML database Component
  • XML slideshow Component
  • XML scroller menu Component
  • Flash - XML Tutorial

    Flash/XML Syntax

    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. Now let´s see what these lines mean. We first create an instance for a new XML file. Like everything else in Flash XML is an object. So we use the constructor new XML(). Let´s give our XML object the instance name myXML. We then load our XML file and the instance is a defined object. And now comes something, which is particular for Flash. In Flash textnodes containing only white space are also recognized as nodes. To exclude those we add a line and set ignoreWhitespace to true. Finally we use a method to make sure our file has been loaded and give it a function name, in this case startItems.

                        function loadXML(xmlFile){
                            button1.onRelease=function(){
                                 showXml(xmlFile);
                            }
                        }
                        loadXML("xml_tutorial_1.xml");
                        
                        myXML = new XML();
                        myXML.load("xmlFile.xml");
                        myXML.ignoreWhite = true;
                        myXML.onLoad = startItems;
    

    Now we have to write the actual function to parse the XML file. We will use our former file again (a reminder). We give the function a boolean argument success in case the file was not loaded.

                        function startItems(success){
                                if(success){
                                     listItems(this.firstChild);
                                }else{
                                     mainField.htmlText="No file loaded!";
                                }
                        }
    

    Inside our function we have another function listItems, which is executed only when the XML file is loaded. The argument is this.firstChild. Instead of this we can also write myXML, since that is what this refers to. firstChild is the rootnode items with all its contents.

    But we want to access what is actually inside the file. And that is shown in the next script.

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

    I deliberately introduce here some other terms that show you how Flash differentiates between the nodes. To access the text inside we write firstItem.firstChild.firstChild.nodeValue, whereby firstItem holds this.firstChild. So actually our text can also be accessed by this this.firstChild.firstChild.firstChild.nodeValue or in other words this(items)(text)(textnode value).

    Before we continue press the button in this movie and you can see how the file is parsed.

    items and text are XML elements and have the nodeType of 1 and the textnode has the nodeType 3. This can be important if we want to access only certain nodetypes. XML elements have a nodeValue of null and only textnodes have a nodeValue in this case This is XML. On the other hand textnodes have a nodeName of null. Check the Flash help file for more properties.

    Now in the next chapter we want to build a small application like a database for examples models. I have made some more advanced tutorials but I want to stay simple in this tutorial.


    PREVIOUS          NEXT