spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / xml / column82

Exploring XML and RSS in Flash

Developer News
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC

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


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data · Controllers: Programming Application Logic
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Sprint Launches Mobile WiMAX Network · Albatron Downsizes with the KI780G Mini-ITX Motherboard · Can't Find a Wi-Fi Network? Make Your Own.

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