XML Data

     

XML has been a hot topic among computer developers for the past few years . Macromedia Flash MX includes an extensive set of functions that allow you to import, export, and process XML data. It is one of the most powerful features of Flash.

Introduction to XML

XML, which stands for Extensible Markup Language, is a simple way of storing a database of information. The data is stored in a simple text file. Tags are used to describe the data, much as they are used to describe the information in an HTML page.

XML looks like HTML. Looking at XML in this light is a good way to quickly understand what XML is about, but you also need to know that XML and HTML are different in many ways. HTML is used to describe a Web page. XML can be used to describe anything at all. HTML has a predefined set of tags such as <P> , <IMG> , and <A> , whereas XML does not have any predefined tags.

Now consider this simple XML document. The following is the inventory of a produce store that has five apples, seven oranges, and two peaches:

 <inventory>     <fruit>         <apples>5</apples>         <oranges>7</oranges>         <peaches>2</peaches>     </fruit> </inventory> 

The tag surrounding the entire document has the name inventory . A well- formed XML document can have only one top-level tag.

The hierarchical structure of an XML document is referred to as a tree . Within the tree, each pair of similarly named tags demarcates a node . The node includes the tags themselves and everything between them. So, all the preceding XML code can be considered as one node, demarcated by the <inventory> tags. Within that node, there are other nodes, demarcated by other tags. Nodes one level down in the hierarchy are child nodes. The inventory node has one child node, fruit . fruit has three child nodes: apples , oranges , and peaches . Even the numbers 5 , 7 , and 2 are nodes. They are children of apples , oranges , and peaches , respectively.

The terms node and element are often used interchangeably. Strictly speaking, however, an element is a type of node. Table 22.1 shows some of the most common node types.

Table 22.1. Common XML Node Types

Node Type

Example

Document type

<!DOCTYPE produce SYSTEM "produce.dtd">

Processing instruction

<?xml version="1.0" encoding="iso-8859-1"?>

Element

<oranges type="navel">5</oranges>

Attribute

type="navel"

Text

5


Each component of the tree structure is defined in a Document Type Definition (DTD) or schema . The DTD describes the structure of the data XML document, and the document is considered valid if it conforms to its DTD.

Flash: A Bit Too Forgiving?

The ActionScript XML parser is not a validating parser: It does not attempt to determine whether a document conforms to its DTD. In fact, Flash doesn't even read DTDs. The Flash parser just reads the DOCTYPE declaration and stores it in the docTypeDecl property; it does not validate the document. This is not a flaw in Flash. Many parsers are non-validating. This choice was made to keep the size of the Flash Player to a minimum. A document that is not valid can still be a legal or well-formed XML document.

In addition, however, the Flash parser accepts some mal-formed XML documents, such as documents with multiple top-level nodes. It would be better if Flash generated errors for such documents, at least warning you that other programs may not accept them.


Nodes like inventory , fruit , and apples are called XML nodes . Nodes like 5 , 7 , and 2 are called text nodes . An XML node has a node name but no value. Instead of a value, it usually has more child nodes. A text node, on the other hand, has no name, but it does have a value.

Figure 22.1 shows a graphical representation of the sample XML document. Each box represents a node. When a box is inside another box, the one inside is a child of the larger box.

Figure 22.1. A graphical representation of the produce store XML document.

graphics/22fig01.gif


The first step toward using XML documents is understanding how to navigate inside one. For instance, inventory is the primary node. fruit is the only child of that node. fruit has three children, the first of which is apples . apples has one child, a text node with the value 5 in it.

So the 5 is the first child of apples , which is the first child of fruit , which is the first child of inventory , which is the first child of the document. Another way to say it is that 5 is the first child of the first child of the first child of the first child.

This description sounds wordy, so you may think that dealing with XML documents means that you'll have to deal with long associations like this. However, there is a way to shorten it. A fundamental rule of XML turns complex documents into simple ones: All XML nodes are, in fact, smaller XML documents.

So think of the apples node as its own document. The primary node is apples , and the child of that node is the text node with 5 in it. You'll use this technique to dissect XML data throughout the rest of the chapter.

Fundamentals of XML

One of the most important aspects of XML is that it is readable by both computers and humans . This makes it different from other database-like files, which are either in binary code or arranged in an unreadable fashion.

So you can easily generate and edit simple XML documents all by yourself, using just a text editor such as Notepad or TextPad. But most of the time, XML documents are created by other programs. Many server-side database programs can send XML data. Some database applications that you run on your local machine have an Export to XML function.

To ensure that Flash will be able to read an XML document, make sure it is well-formed . This means it needs to follow a certain set of rules, some of which are obvious.

All tags in an XML document must have matching closing tags. So <apples> needs </apples> . Plus, these tags need to be in the proper order. So <fruit><apples>2</apples></fruit> is correct, but <fruit><apples>2</fruit></apples> will cause an error and the document will not be parsed correctly.

You can place your entire XML document in one line, with no tabs or line breaks. However, using just one line can make a document that is unreadable by humans. So Flash MX 2004 has the capability to ignore whitespace such as tabs and line breaks.

Tags in XML can have not only values or child nodes, but also attributes. For instance, if you want to specify more information than just oranges in the tag, you can use an attribute to get more specific:

 <oranges type="navel">5</oranges> 

You can use attributes exclusively, with no text node. Here's an example:

 <employee name="John Doe" gender="male"></employee> 

When there is no text node, you can also use an abbreviated syntax that combines the opening and closing tags in a single tag, like this:

 <employee name="John Doe" gender="male" /> 

It's typically considered better XML style (and a file size reducer) to use the l



Using Macromedia Studio MX 2004
Special Edition Using Macromedia Studio MX 2004
ISBN: 0789730421
EAN: 2147483647
Year: N/A
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net