Using XML Data in Flash Movies


Flash Player 5 or higher movies can load (and send) external XML data. This is a very powerful feature, as XML has become a standard data structure for e-commerce purposes and for news services, as well as for easier control over HTML formatting (and style sheets) in the Web browser. You can organize external data with simple XML formatting and use the XML data for text fields and ActionScript code in your Flash movies.

Understanding XML

XML is an acronym for eXtensible Markup Language. Extensible, in this case, means that you can create your own markup tag names and attributes. While there are a few additional rules with XML, its structure very much resembles traditional HTML:

 <tag name opener>Information here</tag name closer> 

For basic XML-Flash usage, your XML document needs one "container" tag in which all other subordinate tags will be nested. Each opener and closer tag set is called a node. In the following XML example, the <section> tag is the primary container tag, and the <article> tags are nodes of the <section> tag:

 <section>      <article>First article node</article>      <article>Second article node</article> </section> 

Note 

Technically, for XML to be to specification, you should have an XML declaration at the very top of your document. Flash Player 5 and higher do not require this declaration, but it's a good habit to add the declaration:

 <?xml version="1.0" encoding=" utf-8" ?> 

For Flash Player 6 and higher, you should make sure that you also save the document as Unicode (UTF-8). In Macromedia Dreamweaver, look for this option under Modify ð Page Properties. In other text editors, look for the Unicode UTF-8 option in the Save As dialog box.

You can create as many child nodes as you need. In the preceding example, the <section> tag has two child nodes: the first occurrence of <article> ... </article> and the second occurrence of <article> ... </article>. In the following example, the first <article> node has two child nodes:

 <section>      <article>         <title>WANTED: New Computer</title>         <description>Insert description here</description>      </article>      <article>Second article node</article> </section> 

<title>...</title> is the first child node of the first <article>...</article> node. The value of <title> is also considered a child of <title>. In the previous example, "WANTED: New Computer" is the child of <title>.

Caution 

Early releases of Flash Player 5 do not ignore XML text nodes that contain only white space. For this reason, you may not want to format your XML documents with indented tags or carriage returns between tags. Otherwise, you will need to create an ActionScript routine that removes the white space. Flash Player 6 and higher support the ignoreWhite property of the XML class, which can tell ActionScript to ignore any text nodes that contain only white space within the XML document.

Loading an XML Document into a Flash Movie

Once you have an XML document structured to use in a Flash movie, you can use the XML document tree in the Flash movie. When an XML document is loaded into a Flash movie, the structure and relationship of all nodes are retained within the Flash Player.

The XML Object

Before you can load an XML document into Flash, you need to make an object that will hold the XML data. To do this, use the XML constructor function, as in:

 var xmlData = new XML();  // ActionScript 1.0 

or

 var xmlData:XML = new XML();  // ActionScript 2.0 

Just as you created new objects for the MovieClipLoader and Sound objects in ActionScript, you can create as many new instances of the XML object as you need for your movie. You can also use an XML object to store Flash-created XML structures and send them to a server for further processing.

The load Method of the XML Object

After you have established an object, like the xmlData variable in the previous heading, you can invoke built-in methods of the XML object. The load() method enables you to specify an external source (such as a URL or filename) that holds the XML data. If you had an XML document called articles.xml in the same directory as your .swf file, you could load it by writing the following code:

 var xmlData:XML = new XML("articles.xml"); 

or

 var xmlData:XML = new XML(); xmlData.load("articles.xml"); 

The onLoad() Method of the XML Object

After the document is loaded into the Flash movie, you can specify another function (or action) to occur using the onLoad() method of the XML object. The onLoad() method simply defines a function to be executed when the XML document is finished loading — it does not actually execute the function (or actions) when the onLoad() is first processed. In the following example, the onLoad() handler is executed when the XML document, articles.xml, is finished loading:

 var xmlData:XML = new XML(); xmlData.onLoad = function(bSuccess:Boolean):Void {    if(bSuccess){       //perform more XML methods upon the XML data    } else {       // indicate that the XML document (or data)       // did not load.    } }; xmlData.load("articles.xml"); 

In the preceding code example, the onLoad() handler has one argument, bSuccess. The onLoad() method is passed a Boolean value of true or false. If the load() method successfully loads the articles.xml document, the onLoad() method will be executed and passed a true value for the bSuccess argument. This true value is inserted into the if condition. If bSuccess is equal to true, the nested if actions will be executed; otherwise, the else actions will be executed.

On the CD-ROM 

Check out the XML document load examples on this book's CD-ROM in the ch29/xml folder. These examples demonstrate how XML node values can be manipulated with Flash arrays. You may want to review Chapter 26's coverage of the Array object before looking at these examples.

Web Resource 

You can find Shane Elliott's expert tutorial, "Using XMLSockets with a Flash Movie," from previous editions of the Flash Bible at www.flashsupport.com/archive. This article introduces you to the basic concepts of using the XMLSocket class in ActionScript.

Colin Moock's expert tutorial, "Unifying the Web," which discusses the concepts behind multiuser server functionality, can also be found at the www.flashsupport.com/archive location.

Web Resource 

We'd like to know what you think about this chapter. Visit www.flashsupport.com/feedback to send us your comments.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net