|
|
|
|
|
|
XML Basics Part III: An Example of Well-Formed and Valid XML
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>
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:
Go to page: 1 2
|
Intranet Journal's Tutorials |
|
Managing Editor |