MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 397

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

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
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."
About loading text and variables into text fields
event
XML.onLoad()
397

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Flash 8

Table of Contents