Recipe 20.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:
-
Create an XML object and populate it by
assigning to it an XML literal.
-
Populate an XML object tree by passing an XML
string to the XML constructor.
-
Create an empty XML object and use E4X 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 ActionScript. 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 create a new XML object and assign an XML literal to
it:
var example:XML = <abc><a>eh</a><b>bee</b><c>see</c></abc>;
This is an example of E4X in action. Notice that
directly inside the code file, an XML packet is used on the
right-hand side of an equals sign. If the XML had been contained in
quotation marks, this would be interpreted as a string, but
ActionScript 3.0 provides native support for XML datatypes. The
ActionScript compiler understands that the expression on the right
is XML, and it populates the example XML instance with the
XML packet provided.
This first technique is appropriate when you
already know the XML structure before you want to create the
object. It is even applicable if you want to use dynamic data
inside of the XML literal. Place a variable name inside of curly
braces ({ and })
to use its value inside the XML literal. For example, if you want
to create a simple XML object to send a user's name and score to
the server, you would construct the XML literal as follows:
// Assume two variables exist, username and score
var username:String = "Darron";
var score:int = 1000;
// Use curly braces around the variable name to use its value when
// assigning XML via an XML literal
var example:XML = <gamescore>
<username>{username}</username>
<score>{score}</score>
</gamescore>;
You can also create a string and pass it as a
parameter to the XML constructor. The preceding example can also be
written as follows:
// Create the XML structure with a string so use the value of both
// username and score inside of the XML packet.
var str:String = "<gamescore><username>" + username + "</username>"
+ "<score>" + score + "</score></gamescore>";
// Pass the string to the constructor to create an XML object
var example:XML = new XML( str );
The example XML objects created in the
two previous code blocks are identical in structure. In general,
use the curly brace approach to create XML objects when you know
what the structure is going to be at compile time but need data at
runtime (like a score that's generated after the game has been
played).
There are other cases 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 his shopping
cart. In such cases, you should construct a new XML object at the
start and use E4X to manipulate it by adding and removing nodes, as
described in Recipes 20.3
and
20.10.
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 to send
data to a server or to use throughout our programs, 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, see
Recipe 20.11 for how to load an XML document from a server.
See Also
Recipes 20.3,
20.4,
20.5,
and
20.10 for more information on constructing an XML tree using
E4X. For more information on loading information from external
sources, see
Recipe 20.11.
|