You want to create an XML object complete with a tree structure and data.
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.
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 in situations in which you already have an existing XML object, but you want to populate it with new data. For example, you might already have an existing score_xml object in which you stored the name and score of the last player. Instead of creating a new object, you can simply repopulate the existing object using parseXML( ). For example:
xmlStr = "<gamescore><username>" + username + "</username><score>" + score + "</score></gamescore>"; score_xml.parseXML(xmlStr);
There are other cases, however, in which you don't necessarily know all the data at one time. You might want to build the XML object's tree over time. For example, if you use an XML object to store a user's shopping cart information, you need to modify the object's data each time the user modifies her shopping cart. In such cases, you should use the built-in properties and methods that allow you to add and remove nodes, as described in Recipe 19.3 and Recipe 19.4.
And let's not forget about populating XML objects with data retrieved from an external source, such as a static XML document or a script that generates dynamic XML. Up to this point we've looked only at constructing XML objects send data to a server. But you can load data from the server for all kinds of reasons?retrieving user data, catalog information, or movie initialization information, just to name a few. For these scenarios, you should use either the load( ) or the sendAndLoad( ) method, as discussed in Recipe 19.11 and Recipe 19.14.
See Recipe 19.3, Recipe 19.4, Recipe 19.5, and Recipe 19.6 for more information on constructing an XML tree using the built-in properties and methods of the XML class. For more information on loading information from external sources, see Recipe 19.11 and Recipe 19.14. Also refer to Recipe 19.16 for an example of how to use loaded data to initialize a movie.