O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

19.11. Loading XML

Problem

You want to load XML data from an XML document or a server-side script that generates XML.

Solution

Use the XML.load( ) method to load the data. Define an onLoad( ) method to handle the results.

Discussion

The load( ) method loads XML data from the URL that you specify:

my_xml = new XML(  );

// Initiate the loading of an XML document in the same directory as the .swf file.
my_xml.load("myXMLDoc.xml");

To meaningfully process the XML data that loads, you must define an onLoad( ) method for the XML object on which you called load( ). The onLoad( ) method is automatically invoked when the data loads, and it is passed a Boolean value indicating whether the XML data was successfully parsed. It is up to you to define the onLoad( ) method so that it processes the XML data in the way that you want it to.

my_xml.onLoad = function (success) {
  if (success) {
    trace("The XML was successfully parsed. Here are the contents: ");
    trace(this);
  } else {
    trace("There was an error parsing the XML data");
  }
};

You should, almost without exception, set the XML object’s ignoreWhite property to true before you load any XML data into a Flash movie. Otherwise, the ActionScript XML parser creates whitespace nodes for any space, tab, carriage return, newline, or form feed character. The resulting data is much more difficult to handle because you have to contend with excess whitespace nodes. Setting the ignoreWhite property to true causes the parser to automatically ignore whitespace and not ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required