Exploring XML and RSS in Flash (3/4) - exploring XML | WebReference

Exploring XML and RSS in Flash (3/4) - exploring XML

Exploring XML and RSS in Flash

Simple Flash API for XML - SFAX

After we discovered how the Actionscript deals with different sorts of XML, we now focus on how an XML document is made available in Flash.

If you're experienced with the W3C Document Object Model, you'll feel familiar with the Flash XML API. Some DOM interfaces are not implemented in the Flash API or do not comply with the DOM ECMAScript Language Binding definition. There is only one type of object to access XML documents and document fragments: XML. It has methods and properties mostly from the W3C DOM Node interface but also from the Document interface (createElement() and createTextNode()).

A comparison of Nodes in W3C XML and SFAX:
Node.nodeType (W3C) XML.nodeType XML.nodeName XML.nodeValue XML.attributes
ELEMENT_NODE 1 tag name null
  • W3C: NamedNodeMap
  • Actionscript: Collection (read-write); returns an associative array containing all attributes of the specified XML object.
TEXT_NODE 3
  • W3C: #text
  • Actionscript: If the XML object is a text node (nodeType == 3), the nodename is null.
content of the text node null

Finding XML Objects: getElementsByTagName()

Both the W3C Document Interface and the Element Interface define a method getElementsByTagname(). It has one argument in the method signature and returns an object of type NodeList.
Our Method is invoked with 3 arguments:

xmlDoc
The document to parse. It must be an instance of an Actionscript's XML object. We dont check this!
tagname
The tagname of the elements we want to find. Should be a String.
xmlObjArray
An Array with XML objects that is populated and returned. If this argument is set to null this array is created inside the method.

and returns an array of XML objects. Let's look at the code:

function getElementsByTagName(xmlDoc, tagname, xmlObjArray){
    var Nodes = null;
    if(xmlObjArray == null){
        Nodes = new Array();
    }else{
        Nodes = xmlObjArray;
    }
    if(xmlDoc.hasChildNodes()){
        for(var i=0; i < xmlDoc.childNodes.length; i++){
            if(xmlDoc.childNodes[i].nodeName == tagname){
                Nodes.push(xmlDoc.childNodes[i]);
            }
            getElementsByTagName(xmlDoc.childNodes[i], tagname, Nodes);
        }
    }
    return Nodes;
}

First we assign the XML objects to a new array. Then we push all child elements of xmlDocwith the name in question onto the array.

If you want to avoid pop-ups like this:

you should declare the counter i in for(var i=0; i < xmlDoc.childNodes.length; i++) with the var "action". The var argument is required when you invoke the function recursivly from inside the for block.

Creating the RSS movie...


Produced by Michael Claßen

URL: http://www.webreference.com/xml/column82/3.html
Created: May 26, 2003
Revised: May 26, 2003