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

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

Exploring XML and RSS in Flash

In this article we will examine the XML processing capabilities of Macromedia Flash, and create an RSS "movie" along the lines of the wildly popular RSS applet.

Since Flash 5, both the authoring tool and the player can process XML. Flash offers possibilities to access and manipulate XML objects with its built-in scripting language, Actionscript (based on the ECMA-262 specification).

One reason for using XML in Actionscript is to separate content from presentation in the Flash movie. This allows you to update the information displayed in your Flash movie without having to change it directly. Separating some content gives you also the option to deal with dynamic data served by an application server. Furthermore, it could simplify the localization of your movie. Let's look at an example for the latter case as an ideal beginning to explore XML capabilities in Flash.

Loading the XML

The best way to explain how loading and parsing works in Actionscript is to look at the code:

var document = new XML();
document.onload = myLoadHandler;
document.load("xml/myskipintrobutton.xml");
function myLoadHandler(success){
    trace(document.loaded.toString())
    trace(success.toString())
    if(success){
        if(document.status == 0) {
            trace(document.toString());
        }else{
            trace("could not parse XML, document.status " + document.status);
            trace(document.toString());
        }
    }else{
        trace("didn't get the xml");
    }
}

First, a new empty XML object is created and gets a load handler assigned. Then the document is loaded. The URL to your XML document should point to a file in the same subdomain where the flash movie is hosted. It's possible to "work around" this security issue by sending a HTTP redirect to a file in another domain, as mentioned in the Flash XML FAQ. Newer versions of Flash players, such as 6.0 r79 in Windows and Linux, have been changed so that a redirect pointing to a file in another domain is no longer valid.

After the document is loaded, the function myLoadHandler(success) is invoked. Actually this function is only a skeleton with some trace() messages used as debugging code, but it's enough to experiment with different XML files, both wellformed and malformed, and different enviroments, standalone and browser-embedded. The first step is to load the XML and trigger some actions when completed.

The official Macromedia documentation describes the behaviour of XML.onLoad in this way:

myXML.onLoad(success);
Method; invoked by the Flash Player when an XML document is received from the server. If the XML document is received successfully, the success argument is true. If the document was not received, or if an error occurred in receiving the response from the server, the success argument is false. The default implementation of this method is not active. To override the default implementation, you must assign a function containing your own actions.

This means the following: External XML documents loaded with the Actionscript functions XML.load(url) and XML.sendAndLoad(url, target) are simply requested via HTTP. If the corresponding HTTP response is completely received, the handler function is invoked. The flag success is true when a document was successfully received. Conversely, this flag is set to false when the document wasn't received with the response, for instance, because of the infamous HTTP "404 not found" error. Also note that success does not reflect the parsing status of the XML document.

The player's behavior for pointing to a local versus an external domain is as follows:

There are reports about time-outs when retrieving large files. Now that we've revealed the secrets of loading XML files in Flash with Actionscript, let's move on to parsing the successfully retrieved XML.

On to parsing the XML...


Produced by Michael Claßen

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