Macromedia Flash 8 Bible Free Open Book

Macromedia Flash 8 Bible

Previous Page
Next Page

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.


Previous Page
Next Page
Index: [A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


     Main Menu
Table of Contents
Back Cover
Macromedia Flash 8 Bible
Foreword
Preface
Part I: An Introduction to Flash Web Production
Part II: Mastering the Flash Environment
Part III: Creating Animation and Effects
Part IV: Integrating Media Files with Flash
Part V: Adding Basic Interactivity to Flash Movies
Part VI: Distributing Flash Movies
Part VII: Approaching ActionScript
Part VIII: Applying ActionScript
Chapter 28: Sharing and Loading Assets
Chapter 29: Sending Data In and Out of Flash
Using Text Fields to Store and Display Data
Defining a Data Process with States
Creating a User Comment Form
Using XML Data in Flash Movies
Summary
Chapter 30: Applying HTML and Text Field Formatting
Chapter 31: Creating a Game in Flash
Chapter 32: Managing and Troubleshooting Flash Movies
Part IX: Integrating Components and Data-Binding
Part X: Expanding Flash
Part XI: Appendixes
Index
List of Figures
List of Tables
List of Listings
List of Sidebars


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele