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.2. Creating an XML Object

Problem

You want to create an XML object complete with a tree structure and data.

Solution

A populated XML object contains data, just as a populated city contains people.

Use one of the following to create a populated XML object:

  • Populate the XML object tree by passing an XML string to the constructor.

  • Create a blank object and use the parseXML( ) method to populate the XML tree with an XML string.

  • Create a blank object and use the XML class’s properties and methods to populate the XML tree one node at a time.

  • Create a blank object and load the XML data from an external source.

Discussion

There are many possibilities for creating a populated XML object in Flash. Each technique offers its own advantages, and you should base your decision on the needs of your project.

The simplest way to create a populated XML object is to pass an XML string to the constructor:

my_xml = new XML("<abc><a>eh</a><b>bee</b><c>see</c>");

This first technique is appropriate when you already know the complete XML string before you want to create the object. For example, if you want to create a simple XML object to send a user’s name and score to the server, you could easily construct the XML string using known data and then construct the XML object, as follows:

xmlStr = "<gamescore><username>" + username + 
         "</username><score>" + score + "</score></gamescore>";
score_xml = new XML(xmlStr);

You can also use the parseXML( ) method to populate an XML object using an XML string. This is useful ...

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