Using XML Data in Flash Movies Flash

Flash 5 movies can now load (and send) external XML data. This is a very powerful feature, as XML has quickly 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>

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> is the first child node of the first <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>.

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:

myXML = new XML();

Just as we created new objects for the Color 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 myXML 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 (as a URL or file name) that holds the XML data. If you had an XML document called articles.xml in the same directory as your .SWF file, then you could load it by writing the following code:

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, a function named loadArticles is executed when the XML document, articles.xml, is finished loading:

In this code example, the loadArticles function has one argument, success. The onLoad method returns a Boolean value of true or false. Therefore, our function loadArticles receives that value, too. If the previous load method successfully loaded the articles.xml document, then the onLoad method will be executed and ill return a true value. This true value is passed to the loadArticles function, and inserted into the if condition. If success is equal to true, then the nested if actions will be executed. Otherwise, the else actions will be executed.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Flash Topics