Recipe 19.1 Understanding XML Structure (Reading and Writing XML)

19.1.1 Problem

You want to understand how to read or write XML.

19.1.2 Solution

XML is tag-based and hierarchical. If you are familiar with HTML, learning the basics of XML isn't very difficult.

19.1.3 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:


You can use XML to tell us who these people are:


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 kinds of nodes are called elements. Also, the values such as Jerry, Carolyn, and Laura are nodes. These kinds of 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.

  • 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, and not elements. In either case, 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.

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 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 larger quantities of data, when the data is long (more than a few words), or when the names of the attributes 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.
Blah blah blah.

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:

xmlStr = "<feedback name='Aham' 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 using a server-side script, consult the appropriate reference for the language you are using.

19.1.4 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 Flash. For more information on XML in general, see For more information on XML data structures and how to access them in Flash, refer to the XML, XMLnode, and XMLSocket classes in the "Language Reference" section of ActionScript for Flash MX: The Definitive Guide.

    Part I: Local Recipes
    Part II: Remote Recipes