EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 397

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
About loading text and variables into text fields 397
Loading and displaying text from an XML document
XML data is a popular way to distribute content on the Internet, in part because it is a widely
accepted standard for organizing and parsing data. As such, XML is an excellent choice for
sending and receiving data from Flash; however, XML is slightly more difficult to learn than
using LoadVars and FlashVars to load data and display text.
To load text into Flash from an external XML document:
1. Create a new Flash document and save it as xmlReviews.fla.
2. Add the following code to Frame 1 of the Timeline:
this.createTextField("my_txt", 10, 10, 10, 320, 100);
my_txt.autoSize = "left";
my_txt.border = true;
my_txt.multiline = true;
my_txt.wordWrap = true;
var reviews_xml:XML = new XML();
reviews_xml.ignoreWhite = true;
reviews_xml.onLoad = function (success:Boolean):Void {
if (success) {
var childItems:Array = reviews_xml.firstChild.childNodes;
for (var i:Number = 0; i < childItems.length; i++) {
my_txt.text += childItems[i].firstChild.firstChild.nodeValue +
"\n";
}
} else {
my_txt.text = "Unable to load external file.";
}
}
reviews_xml.load("http://www.helpexamples.com/flash/xml/reviews.xml");
The first block of code in the preceding snippet creates a new text field on the Stage. This
text field is used to display various parts of the XML document that is loaded later. The
second block of code handles creating an XML object that will be used to load the XML
content. Once the date is completely loaded and parsed by Flash, the
XML.onLoad() event
handler is invoked and displays the contents of the XML packet in the text field.
3. Save the Flash document and select Control > Test Movie to test the SWF file.
Flash displays the following output in the text field on the Stage:
Item 1
Item 2
...
Item 8
For information on security, see Chapter 17, “Understanding Security.

Table of Contents

Related product manuals