Accessibility
 
Home / Developer Center / Macromedia Flash Developer Center /

Macromedia Flash Article

Icon or Spacer Icon or Spacer Icon or Spacer
Helen Triolo
 
Helen Triolo
flash-creations.com
 
Tools: XML object
 

XML (Extensible Markup Language) is a way of defining data using tags that are similar to HTML tags, but not predefined as HTML tags are. Instead, you make up the tags (or use one of the evolving XML application standards) which best define your data structure. Providing you follow the correct XML structure (tags nested within others, and all entries having both an opening and closing tag), Macromedia Flash can read files that are written with these XML tags. Flash does not use DTDs (Document Type Definition files), which are used by other XML-reading applications. Instead, Flash just allows you to read XML-structured data into a Flash XML object, which has predefined methods and properties associated with it.

 

Example XML document
Here's an example of an XML document which was obtained from the news categories at moreover.com (from the Environment News section of Science). The green part is the actual document, which was truncated to remove all the repeating <article> entries and the closing </moreovernews> tag. The rest is annotation which shows how the various parts (nodes) relate to Flash. Following the graphic, we'll explain how to get the contents of that document into a Flash XML object.

XML document example

 

A note about whitespace
In the original version (r30) of the Macromedia Flash Player 5, any whitespace in an XML file (carriage returns, spaces between tags) was read in by Flash as a node. In the most recent versions of the player (r41 in Netscape, r42 in Internet Explorer), a method has been added to the XML object, ignoreWhite, which allows whitespace to be ignored. Thus if your Flash movie is running in a browser with the r41 or r42 player installed, and you include xmlobject.ignoreWhite=true in your file, you'll have no problem with whitespace. But if you want to test your code in the Flash environment (Ctrl, Test Movie), which still uses the r30 player, or make sure that it runs in an online application with all versions of the Flash player, you'll need to do one of the following:

>

Download Branden Hall's XML Nitro routine, which makes use of an undocumented AS function to parse XML objects and makes the ignoreWhite property of the XML object available in all versions of the Flash 5 player. Save it on your hard drive as xmlnitro2.as. Then you can just use an #include statement at the beginning of your code to parse in the XML object.

>

Include a routine which manually strips whitespace from the incoming XML document. Colin Moock has posted one at his ASDG site, in the Code Depot section, strip xml whitespace.

>

Format the xml file to remove any space and carriage returns between tags.

 

Steps to read contents of an XML file into a Flash XML object
Reading in XML data requires these steps:

1

Define the function that will be executed when the data has loaded (function myLoad in the example)

2

Create a new XML object (thisXML)

3

Set ignoreWhite to true (so Flash won't treat carriage returns and other whitespace in the xml file as separate nodes -- only works in r41+ revisions of Flash 5 if xmlnitro.as not used in conjunction)

4

Tell Flash which function will be used when the data has loaded

5

Load the data!

 

An example using the XML file shown above
In the example below, the function that is called when the xml file has been loaded does a trace on various node properties, child nodes, and properties of those child nodes. Comparing the output below with the file above and looking at the code should give you some idea of how various read functions of the XML object work.

#include "xmlnitro2.as"
function myLoad () {
 // This function gets executed when Flash has
 // loaded the complete XML file. It displays
 // node properties for the first 4 levels of
 // nodes found in the code, this = the XML
 // object that called this function
 // (eg this.firstChild = the first child node of thisXML)
 trace("xml declaration: " + this.xmlDecl);
 trace("xml doctype: " + this.docTypeDecl);
 trace("number of articles: " + this.firstChild.childNodes.length);
 trace(" ");
 level1Child = this.firstChild;
 level2Child = level1Child.firstChild;
 level3Child = level2Child.firstChild;
 level4Child = level3Child.firstChild;
 // note that this last statement could also have been written
 // level4Child = this.firstChild.firstChild.firstChild.firstChild;
 // or
 // level4Child =
 // this.childNodes[0].childNodes[0].childNodes[0].childNodes[0];
 trace("level1: name=" + level1Child.nodeName);
 trace(" value=" + level1Child.nodeValue);
 trace(" type=" + level1Child.nodeType);
 trace(" ");
 trace("level2: name=" + level2Child.nodeName);
 trace(" value=" + level2Child.nodeValue);
 trace(" type=" + level2Child.nodeType);
 // the attributes property is itself an object
 for (attr in level2Child.attributes) {
  trace(" attributes=" + attr + " value=" +
  level2Child.attributes[attr]);
 }
 trace(" ");
 trace("level3: name=" + level3Child.nodeName);
 trace(" value=" + level3Child.nodeValue);
 trace(" type=" + level3Child.nodeType);
 // the following will not produce any output
 // because level3Child has no attributes
 for (attr in level3Child.attributes) {
  trace(" attributes=" + attr + " value=" +
  level3Child.attributes[attr]);
 }
 trace(" ");
 trace("level4: name=" + level4Child.nodeName);
 trace(" value=" + level4Child.nodeValue);
 trace(" type=" + level4Child.nodeType);
 trace(" ");
 trace(level3Child.nextSibling.nodeName);
 trace(level3Child.nextSibling.firstChild.nodeValue);
 trace(" ");
 n3 = level2Child.childNodes.length;
 trace("number of level3 nodes = " + n3);
 trace(" 1st level3 node = " + level2Child.childNodes[0].nodeName);
 trace(" 2nd level3 node = " + level2Child.childNodes[1].nodeName);
 trace(" 3rd level3 node = " + level2Child.childNodes[2].nodeName);
 trace(" last level3 node = " + level2Child.childNodes[n3-1].nodeName);
}
thisXML = new XML();
thisXML.ignoreWhite = true;
thisXML.onLoad = myLoad;
thisXML.load("environment_page.xml");

The output from the code above follows. Notice that even text between tags is treated as a node. It is a node with nodeType = 3 and its value is contained in the XML property nodeValue. The nodeName property is meaningless (=null) for these nodes.

Nodes which look like tags, on the other hand, have a nodeValue of null, a nodeName which is the tag itself, and are nodeType 1, as shown in the output below.

xml declaration: <?xml version="1.0" encoding="iso-8859-1"?>
xml doctype:
<!DOCTYPE moreovernews SYSTEM "http://p.moreover.com/xml_dtds/moreovernews.dtd">
number of articles: 20

level1: name=moreovernews
value=null
type=1

level2: name=article
value=null
type=1
attributes=id value=_22212934

level3: name=url
value=null
type=1

level4: name=null
value=http://c.moreover.com/click/here.pl?x22212932
type=3

headline_text
Green Party supports logging venture

number of level3 nodes = 10
1st level3 node = url
2nd level3 node = headline_text
3rd level3 node = source
last level3 node = access_status

To try this for yourself, grab one of the moreover.com news files from the link above. Get Branden's xmlnitro2.as routine. Cut and paste the code above into the Frame Actions for frame 1 of a blank movie, changing the filename in the thisXML.load statement appropriately, and give it a try! Then modify as you like.

 

 

 


About the author
Helen Triolo is the owner of i-Technica, a web design shop in the Washington DC area. An electrical engineer turned-programmer turned-web designer, she became fascinated with Flash after obtaining a copy of Flash 4 and discovering the limitless possibilities for programmable animation that it offered. Addicted ever since, she's used Flash in a variety of client websites and CDs, posted an archive of Flash Q&A's and ActionScript tutorials, and teaches and writes about ActionScript whenever possible.