Reading Time: 2 minutes

Loading XML data into Flash presentations allows you to create dynamic and easily updatable content. By modifying only the XML file, you can change the data displayed in a Flash movie without editing the Flash source itself.

Understanding how Flash loads and processes XML data makes it possible to build flexible presentations where content can be updated or replaced as needed.

Why Use XML with Flash

Using XML as a data source for Flash presentations offers several advantages:

  • Separation of content and presentation
  • Easy content updates without recompiling the Flash movie
  • Support for structured and hierarchical data
  • Dynamic presentation behavior

Flash provides a built-in XML object that can be used to load and parse external XML files.

Sample XML File

Consider the following XML file containing a list of books. This data will be loaded into the Flash movie and displayed dynamically.


<?xml version="1.0"?>
<books>
  <item>
    <bookname>Head Rush Ajax</bookname>
    <author>Brett McLaughlin</author>
  </item>
  <item>
    <bookname>Exalted</bookname>
    <author>Alan Alexander</author>
  </item>
</books>

Save this file as books.xml in the same directory as your Flash movie.

Preparing the Flash Movie

Create a new Flash movie and add text fields using the Text Tool. From the Properties panel, set the text fields to Dynamic Text.

For this example, create two text fields:

  • bookname_txt – to display the book title
  • author_txt – to display the author name

Ensure the width of the text fields is sufficient for the expected text. If the text may span multiple lines, set the line type to Multiline.

ActionScript Code to Load XML Data

In the first frame of your Flash movie, open the Actions panel by pressing F9 and add the following code:


function loadXMLData(loaded) {
  if (loaded) {
    _root.bookname = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
    _root.author = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;

    bookname_txt.text = _root.bookname;
    author_txt.text = _root.author;
  } else {
    trace("Could not load XML file");
  }
}

xmlFile = new XML();
xmlFile.ignoreWhite = true;
xmlFile.onLoad = loadXMLData;
xmlFile.load("books.xml");

How the Code Works

The code creates an XML object and loads the external books.xml file. When the file finishes loading, the onLoad event triggers the
loadXMLData function.

If the XML file loads successfully, the code navigates through the XML structure to extract the values of the bookname and author elements.

These values are then assigned to the corresponding dynamic text fields in the Flash movie.

Handling Whitespace

The ignoreWhite property is set to true to prevent Flash from reading whitespace nodes in the XML file. This is especially important in older versions of Flash that treat whitespace as separate nodes.

File Path Considerations

When calling the load method, the correct path to the XML file must be specified. In this example, the XML file and the Flash movie are assumed to be in the same directory, so only the file name is used.

Extending the Example

This basic example demonstrates how to load and display XML data in Flash. You can extend this logic to:

  • Display multiple XML records
  • Add Next and Previous buttons
  • Navigate through XML nodes dynamically
  • Create interactive presentations

By modifying the loadXMLData function and adding navigation controls, you can display XML data sequentially based on user interaction.

Conclusion

Loading XML data into Flash presentations enables dynamic, data-driven content that is easy to maintain and update. By separating data from presentation, developers can build flexible Flash applications that adapt to changing content requirements.