Recipe 20.1.
Understanding XML Structure (Reading and Writing XML)
Problem
You want to understand how to read or write
XML.
Solution
XML is tag-based and hierarchical. If you are
familiar with HTML, learning the basics of XML should not be very
difficult.
Discussion
Although reading and writing good XML is not a
skill that is specific to ActionScript, it is, nonetheless, a skill
from which your ActionScript can benefit. If you are not yet
familiar with XML, don't worry. This is going to be painless.
XML is a way of representing structured data.
This means that you explicitly define the context for the data. For
example, without XML you might have a string of data such as:
Jerry,Carolyn,Laura
You can use XML to tell us who these people are:
<family>
<father>Jerry</father>
<mother>Carolyn</mother>
<sister>Laura</sister>
</family>
Now, as you can see, the XML tells us a lot more
about the data. Here are a few other points to notice about
XML:
-
XML is composed mainly of nodes. A node is a general term
that can refer to many parts within the XML. For example,
<family> is a node in the preceding XML snippet;
these nodes are called elements. Also, the values Jerry,
Carolyn, and Laura are nodes; these nodes are called text
nodes.
-
Every XML element must have a matching opening and closing
tag. The opening tag might look like <family>, and
the closing tag is identical, except that it uses a forward slash
to indicate it is closing the element, as in
</family>. The opening and closing tags can be
combined if the element does not contain any nested nodes. For
example, <emptyElement /> is an element
that combines the opening and closing tags. Notice that there is a
space between the element name, emptyElement, and the
forward slash. The space isn't necessary; it's just a matter of
personal style.
-
Elements can contain nested nodes (be they other elements or text
nodes). There are several examples of this in the
<family> XML document shown earlier. The
<family> element, which is the root node in
this example, contains three nested elements:
<father>, <mother>, and
<sister>; these nested nodes are also called
child nodes. Each of these child nodes also contains a
nested node. However, their nested nodes are text nodes, not
elements. Regardless, they are still treated as child nodes.
There is one other type of node that we want to look at here. An
attribute is a special kind of node that can be assigned to
an element, and in many cases it can even be used as an alternative
to a nested node. If you've ever worked with HTML, you are already
familiar with attributes. Some common attributes in HTML include
the HRef attribute of the <a> element and
the colspan attribute of the <td> element.
Here is how we can rewrite the same XML document we examined
previously using attributes instead of nested nodes:
<family father="Jerry" mother="Carolyn" sister="Laura" />
Notice that we were able to eliminate the nested
elements and write the same data all in one element. Also notice
that since <family> no longer contains any nested
nodes, we can combine the opening and closing tags into one.
You may be wondering when and why to use attributes versus
nested nodes. This is often a matter of preference. Sometimes it
may appear easier or clearer to you to write the XML data by using
attributes. Generally, attributes are a good idea when you want to
represent a fairly small number of values and when those values are
relatively short. Also, the attributes' names need to be unique
within the element. When you want to represent a larger amount of
data, such as when the data is long (more than a few words) or when
the attribute names would not be unique within the element, use
nested elements instead.
Also, you can use a combination of both
attributes and nested nodes. Here is an example of an
<article> element that includes attributes for the
title and author, but uses a nested text node to
represent the article text. This is a good example of when one of
the values (the article text) is simply too long to reasonably be
an attribute.
<article title="XML: It's Not Just for Geeks" author="Samuel R.
Shimowitz">
My friends couldn't believe it when I started working with XML.
I became an outcast, confined to my dark office illuminated only
by the glow of my trusty CRT.
</article>
You can create XML strings right in Flash for
cases in which you are constructing XML data to send to an external
script. For example:
var dataToSend:String = "<feedback name='Anon' comments='nice'>";
To construct a static XML document outside of
Flash, use a simple text editor and save it as plain text. To
create an XML document dynamically by using a server-side script,
consult the appropriate reference for the language you are
using.
See Also
The preceding examples are very simple cases.
XML can get much more complex, including namespaces, document type
declarations, etc. Most of these details are well beyond the scope
of this book and beyond what you need to know to work with XML and
ActionScript. For more information on XML in general, see http://www.xml.com. Also, for a general
overview of XML, you might consider XML
in a Nutshell by Elliotte Rusty Harold and W. Scott Means
(O'Reilly) and/or the XML Pocket
Reference by Simon St.Laurent and Michael Fitzgerald (O'Reilly).
|