XML

Constructing Your First XML Document

You now have the basic knowledge necessary to create an XML document of your own. You may already have in mind some data that you'd like to format into an XML document, which by all means I encourage you to pursue. For now, however, I'd like to provide some data that you can use to work through the creation of a simple XML document. I am involved with a toy and game company called Stalefish Labs, which designs and produces traditional board games, social games, toys, and even mobile software games. We released a traditional print version of a trivia game called Tall Tales a couple of years ago , and we're in the process of putting together a software version of the game. It only makes sense to code the database of questions and answers in XML. The idea is to use XML documents to provide trivia questions to web and mobile versions of the game. By using XML, we'll be able to code the content in a consistent format and easily provide updates and new content periodically.

Anyway, the point of all this game stuff is that a good example of an XML document is one that allows you to store trivia questions and answers in a structured format. My trivia game, Tall Tales, involves several different kinds of questions, but one of the main question types is called a "tall tale" in the game. It's a multiple-choice question consisting of three possible answers. Knowing this, it stands to reason that the XML document will need a means of representing each question plus three different answers. Keep in mind, however, that in order for the answers to have any meaning, you must also provide a means of specifying the correct answer. A good place to do this is in the main element for each question/answer group.

Don't forget that earlier in this tutorial you learned that every XML document must have a root element. In this case, the root element is named talltales to match the name of the game. Within the talltales element you know that there will be several questions, each of which has three possible answers. Let's code each question with an element named question and each of the three answers with the three letters a, b, and c. It's important to group each question with its respective answers, so you'll need an additional element for this. Let's call this element tt to indicate that the question type is a tall tale. The only remaining piece of information is the correct answer, which can be conveniently identified as an attribute of the tt element. Let's call this attribute answer. Just in case I went a little too fast with this description of the Tall Tales document, let's recap the explanation with the following list of elements that are used to describe pieces of information within the document:

  • talltales The root element of the document

  • tt A tall tale question and its associated answers

  • question A question

  • a The first possible answer to a question

  • b The second possible answer to a question

  • c The third possible answer to a question

In addition to these elements, an attribute named answer is used with the tt element to indicate which of the three answers (a, b, or c) is correct. Also, don't forget that the document must begin with an XML declaration. With this information in mind, take a look at the following code for a complete tt element:

<tt answer="a">
  <question>
    In 1994, a man had an accident while robbing a pizza restaurant in Akron,
    Ohio, that resulted in his arrest. What happened to him?
  </question>
  <a>He slipped on a patch of grease on the floor and knocked himself out.</a>
  <b>He backed into a police car while attempting to drive off.</b>
  <c>He choked on a breadstick that he had grabbed as he was running out.</c>
</tt>

This code reveals how a question and its related answers are grouped within a tt element. The answer attribute indicates that the first answer (a) is the correct one.

All of the elements in this example are nonempty, which is evident by the fact that they all either contain text content or additional elements. Notice also how every opening tag has a matching closing tag and how the elements are all nested properly within each other. Now that you understand the code for a single question, check out Listing 2.2, a complete XML document that includes three trivia questions.

Listing 2.2. The Tall Tales Sample XML Document
 1: <?xml version="1.0"?>
 2:
 3: <talltales>
 4:   <tt answer="a">
 5:     <question>
 6:       In 1994, a man had an accident while robbing a pizza restaurant in
 7:       Akron, Ohio, that resulted in his arrest. What happened to him?
 8:     </question>
 9:     <a>He slipped on a patch of grease on the floor and knocked himself out.</a>
10:     <b>He backed into a police car while attempting to drive off.</b>
11:     <c>He choked on a breadstick that he had grabbed as he was running out.</c>
12:   </tt>
13:
14:   <tt answer="c">
15:     <question>
16:       In 1993, a man was charged with burglary in Martinsville, Indiana,
17:       after the homeowners discovered his presence. How were the homeowners
18:       alerted to his presence?
19:     </question>
20:     <a>He had rung the doorbell before entering.</a>
21:     <b>He had rattled some pots and pans while making himself a waffle in
        their kitchen.</b>
22:     <c>He was playing their piano.</c>
23:   </tt>
24:
25:   <tt answer="a">
26:     <question>
27:       In 1994, the Nestle UK food company was fined for injuries suffered
28:       by a 36 year-old employee at a plant in York, England. What happened
29:       to the man?
30:     </question>
31:     <a>He fell in a giant mixing bowl and was whipped for over a minute.</a>
32:     <b>He developed an ulcer while working as a candy bar tester.</b>
33:     <c>He was hit in the head with a large piece of flying chocolate.</c>
34:   </tt>
35: </talltales>

Although this may appear to be a lot of code at first, upon closer inspection you'll notice that most of the code is simply the content of the trivia questions and answers. The XML tags should all make sense to you given the earlier explanation of the Tall Tales trivia data. You now have your first complete XML document that has some pretty interesting content ready to be processed and served up for viewing. Be sure to take a look back at Selecting an XML Editor to see how this exact same code appears within the Butterfly XML WYSIWYM XML editor.