|
Free Open Book
Macromedia Flash 8 Bible |
Using XML Data in Flash MoviesFlash 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 XMLXML 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: 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>...</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>.
Loading an XML Document into a Flash MovieOnce 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 ObjectBefore 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 ObjectAfter 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 ObjectAfter 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.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |