XML Basics Part III: An Example of Well-Formed and Valid XML
P.G. Daly
2/10/2004
Go to page: 1 2
Printer Friendly Version
If you've been following along with Part I and Part II of XML Basics, you're ready for Part III, where you put the pieces together and experiment with hands-on with XML.
As mentioned earlier, XML is hierarchical, and as such the proper nesting of tag elements is crucial. In the example we are going to walk through, I am going to build a pretend movie catalog using XML. To understand the structure and see what we'll be creating take a peek at the following illustration:
From this diagram, you can see that <MovieCatalog> is the root element and will appear only once. In addition, for each movie, the elements such as <length> and <title> will appear only once. Within the document, however, there will be multiple <movie> tags each with its own set of child tags (<length>, <genre>, <actors>, etc...). The tag <actors> will not have any content within it; it will simply contain one or more <actor> child tags.
With this hierarchy in mind, we are now ready to begin creating an XML document. Using Notepad or any other text editor, create a blank new file called "MovieCatalog.xml" and save it to your hard drive. Once we add substance to the XML file, we will be using Internet Explorer to view the results.
Type the following into your XML file:
<?xml version="1.0"?>
All XML files start with this statement as discussed in Part II of this series. Then, type the following, which constitutes the meat of our file.
<MovieCatalog>
<movie>
<title>The Matrix</title>
<length>136</length>
<genre>Sci-Fi and Fantasy</genre>
<actors>
<actor>Keanu Reeves</actor>
<actor>Laurence Fishburne</actor>
<actor>Carrie Ann Moss</actor>
</actors>
<datereleased>1999</datereleased>
<director>Wachowski Brothers</director>
<format>DVD</format>
</movie>
<movie>
<title>Titanic</title>
<length>194</length>
<genre>Drama</genre>
<actors>
<actor>Leonardo DiCaprio</actor>
<actor>Kate Winslet</actor>
</actors>
<datereleased>1999</datereleased>
<director>James Cameron</director>
<format>DVD</format>
</movie>
<movie>
<title>The Sixth Sense</title>
<length>106</length>
<genre>Thriller</genre>
<actors>
<actor>Bruce Willis</actor>
<actor>Haley Joel Osment</actor>
</actors>
<datereleased>1999</datereleased>
<director>M. Night Shyamalan</director>
<format>VHS</format>
</movie>
</MovieCatalog>
Once you save the file, you can open the moviecatalog.xml file in Internet Explorer. If you typed everything correctly, you should see the following:
Internet Explorer automatically parses the file and you can easily see the hierarchy as well as expand and collapse based on the hierarchy. At its simplest state (everything collapsed), this document would look as follows:
You can see part of the power of XML is being able to utilize the structure to your advantage to selectively choose the elements and content you wish to display and with which you want to work. Once you work with some XSL, this distinction becomes more clear. You can choose your formatting based on individual elements, output medium (Web, e-mail, etc.) or a combination of both.
This example clearly illustrates and follows the following rules of elements that were explained in Part II of this article series:
- Every start-tag must have a matching end-tag
- Tags cannot overlap. Proper nesting is required
- XML documents can only have one root element
- Element names must obey XML naming conventions
- XML is case sensitive
- XML preserves white space within text
- Elements may contain attributes. If an attribute is present, it must have a value, even if it is an empty string "".
Go to page: 1 2
Printer Friendly Version