What Is XML?

 <  Day Day Up  >  

XML is a markup language much like Hypertext Markup Language (HTML); they are both derived from the Standard Generalized Markup Language (SGML) . XML is used to describe data. The goals of XML are different than the goals of HTML. One fundamental difference between HTML and XML is that XML tags are not predefined. XML was designed to describe data and to focus on the information contained in the data. HTML was designed to display data and to focus on how it looks.

We could say XML is about describing information. To describe this information, XML uses a Document Type Definition (DTD) or an XML schema. XML with a DTD or XML schema is self-descriptive, meaning that if you have an XML document and you have its DTD, you can interpret the data. The DTD is like a road map for the data. XML is used to store and send information while the DTD reveals the structure.

Listing 12.3 shows an HTML segment that displays a peach cobbler recipe. Notice that HTML is used to display the content using header tags and a table structure.

Listing 12.3. HTML Version of Recipe
 <h1>Peach Cobbler</h1> <h2> Sam Smyth </h2> <P>Peach cobbler is made with bananas as the main sweetener. It was delicious.</P>   <table>   <tr><td> 2 1/2 cups</td> <td> diced Peach</td></tr>   <tr><td> 2 tablespoons</td>  <td> sugar</td></tr>   <tr><td> 2 </td> <td> fairly ripe bananas</td></tr>   <tr><td> 1/4 teaspoon</td> <td> cinnamon</td></tr>   <tr><td> dash of</td>  <td> nutmeg</td></tr>   </table> Combine all and use as cobbler, pie, or crisp. 

With XML, we can define our own "recipe markup language," where the markup tags directly correspond to entities, such as ingredients and amount, in the world of recipes. In Listing 12.4, we have translated the HTML recipe from Listing 12.3. Notice that there are no markup tags for display in the XML.

Listing 12.4. XML Version of Recipe
 <recipe id="117" category="dessert">   <title>Peach Cobbler</title>   <author>Sam Smyth </author>   <description>     Peach Cobbler made with bananas as the main sweetener.     It was delicious.   </description>   <ingredients>     <item><amount>2 1/2 cups</amount><type>diced Peach </type></item>     <item><amount>2 tablespoons</amount><type>sugar</type></item>     <item><amount>2</amount><type>fairly ripe bananas</type></item>     <item><amount>1/4 teaspoon</amount><type>cinnamon</type></item>     <item><amount>dash of</amount><type>nutmeg</type></item>   </ingredients>   <preparation>     Combine all and use as cobbler, pie, or crisp.   </preparation> </recipe> 

An XML document is an ordered, labeled tree. Text nodes contain the actual data (text strings), which are usually character data nodes and must not be empty.

Element nodes are each labeled with a name (often called the element type ) and a set of attributes, each consisting of a name and value. These nodes can have child nodes.

In Figure 12.1, the recipe tree is detailed where the root node is a collection of "Recipes," and each subsequent recipe node contains a series of element and text nodes that comprise the recipe. Sticking to the tree metaphor, an XML document can be traversed recursively, allowing us to read data without knowing if it even exists. Each tree node has reference to its children (or branches), and those children to their children, and so on.

Figure 12.1. XML list of recipes.

graphics/12fig01.gif

Figure 12.2 shows a browser interpretation of an XML file. Note the expandable list icons, indicating that the tree can be expanded and collapsed , revealing the structure.

Figure 12.2. Recipe XML in Internet Explorer.

graphics/12fig02.gif

The recipe XML is an example of XML data represented as text nodes, where the data is text between an opening XML tag and a closing XML tag, as in <author>Sam Smyth </author> . Data can also be represented in XML using tag attributes. Using tag attributes can make the final XML file smaller and therefore more efficient. Listing 12.5 demonstrates an XML file using tag attributes.

Listing 12.5. XML Using Attributes
 <billingData>       <row act="10" custId="0" date="05/12/2002" billable="true" />       <row act="9" custId="0" date="05/13/2002" billable="true"  />       <row act="8" custId="0" date="05/14/2002" billable="false" /> </billingData> 

Notice that each of the row tags contains attributes in name-value pairs. Listing 12.6 shows a single row node from Listing 12.5 using the text node XML model. The size of a single row is greater when using text nodes. If the XML contained many rows, the file would be large. In this case, using attributes would be a better choice.

Listing 12.6. XML Using Attributes
 <row>       <act>10</act>       <custId>0</custId>       <date>05/12/2002</date>       <billable>true</billable> </row> 

The examples throughout this chapter will use tag attributes to contain the data in XML files.

 <  Day Day Up  >  


Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

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