[ Team LiB ] |
Recipe 19.1 Understanding XML Structure (Reading and Writing XML)19.1.1 ProblemYou want to understand how to read or write XML. 19.1.2 SolutionXML is tag-based and hierarchical. If you are familiar with HTML, learning the basics of XML isn't very difficult. 19.1.3 DiscussionAlthough 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:
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. </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: 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 AlsoThe 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 http://www.xml.com. 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. |
[ Team LiB ] |