The name XML comes from the full name of the language, Extensible Markup Language. Although markup is in the name of the language, do not think of XML as you do HTML because, aside from the fact that both languages are based on tag pairs, there are no similarities. XML is used for the storage and exchange of data within its tag pairs, whereas HTML couldn't care less what is contained in the content or how it is structuredits only purpose is to display the content to the browser. In other words, XML is used to define and carry the content, whereas HTML is used to make it visually appealing to the reader.
Basic XML Document Structure
XML documents contain two major elements, the prolog and the body. The prolog contains the XML declaration statement, and any processing instructions and comments you want to add.
By the Way
For a complete definition of XML documents, read the XML specification at http://www.w3.org/TR/REC-xml.
The following snippet is a valid prolog:
<?xml version="1.0" ?> <!-- Sample XML document -->
After the prolog comes the content structure. XML is hierarchical, like a bookbooks have titles and chapters, each of which contain paragraphs, and so forth. There is only one root element in an XML document. Continuing the book example, the element might be called Books, and the tags <Books></Books> surround all other information.
Next, add any subsequent elementscalled childrento your document. Continuing the book example, you will need a master book element and then within it elements for title, author, and publishing information. Call these child elements Title, Author, and PublishingInfo. But the publishing information will likely contain more than one bit of informationyou'll need a publisher's name, location, and year of publication. Not a problemjust create another set of child elements within your parent element (which also happens to be a child elements of the root element). For example, just the <PublishingInfo> element could look like this:
<PublishingInfo> <PublisherName>Joe SystemGod</PublisherName> <PublisherCity>Joe SystemGod</PublisherCity> <PublishedYear>Joe SystemGod</PublishedYear> </PublishingInfo>
All together, a sample books.xml document with one entry could look something like this:
<?xml version="1.0" ?> <!--Sample XML document --> <Books> <Book> <Title>A Very Good Book</Title> <Author>Jane Doe</Author> <PublishingInfo> <PublisherName>Sams Publishing</PublisherName> <PublisherCity>Indianapolis</PublisherCity> <PublishedYear>2006</PublishedYear> </PublishingInfo> </Book> </Books>
Keep in mind two important rules for creating valid XML documents:
Add some dummy entries to the books.xml file and place it in the document root of your web server for use in later examples. You will use the same XML file throughout the different interface examples shown in the rest of this chapter.