An Introduction to XML - Part 2

Written by:  • Edited by: Michele McDonough
Updated Jul 5, 2011
• Related Guides: Sun Microsystems | XML | HTML

This article presents the second part of An Introduction to XML, with a brief look at how XML differs from HTML.

XML Versus HTML

XML and HTML are siblings, yet they are very different.

Female Boxer
click to enlarge
Like HTML, XML is text-based, and uses tags, elements, and attributes. However, the focus of HTML is on the display of content, whereas XML is designed to emphasize the content itself. Because HTML's strength is in displaying and formatting information, you are not able to use it for creating or modifying content like you can with XML. Neither is it extensible in the way XML is, because you cannot add tags.

Table Example

For example, how would you code a catalog of books in HTML? Perhaps you would use a table:

<TABLE>

<TR>

<TH>Author</TH>

<TH>Title</TH>

</TR>

<TR>

<TD>Honore de Balzac</TD>

<TD>Charles Dickens</TD>

</TR>

<TR>

<TD>Cousin Bette</TD>

<TD>Little Dorrit</TD>

</TR>

</TABLE>

The HTML document presents the data in rows and columns, and we may easily add extra formatting to control font size, cell color, and other markups. The tags format the information, but cannot describe it, and are incapable of being altered to meet specific requirements. Additionally, they are the same for every document.

Consider the same information displayed in XML format:

<CATALOG>

<BOOK>

<AUTHOR>Honore de Balzac</AUTHOR>

<TITLE>Cousin Bette</TITLE>

</BOOK>

<BOOK>

<AUTHOR>Charles Dickens</AUTHOR>

<TITLE>Little Dorrit</TITLE>

</BOOK>

</CATALOG>

In contradistinction to the HTML code above, the XML document is focused on adding structure to the data in the form of elements. The elements describe what the data actually is. We know that our document must be a book catalog and that it must specify authors and titles.

An Example Using Lists

Now let's consider a list with a partial table of contents rendered in HTML:

<H2>Tales of Unrest</H2>

<H3>Joseph Conrad</H3>

<OL>Contents

<LI>Karain: A Memory</LI>

<LI>The Idiots</LI>

</OL>

In XML, one solution from many possibilities could be:

<TITLE>Tales of Unrest</TITLE>

<AUTHOR>Joseph Conrad</AUTHOR>

<CONTENTS>

<CHAPTER>Karain: A Memory</CHAPTER>

<CHAPTER>The Idiots</CHAPTER>

</CONTENTS>

So we can observe that the main focus of the XML code is on the content, and not on the presentation per se. The presentation of an XML document will be addressed in a future article.

For Further Reading

The best way to learn XML is by doing as many exercises as possible. Some texts you may wish to consider are:

  • XML: Visual QuickStart Guide (2nd Edition) by Kevin Howard Goldberg
  • New Perspectives on XML by Patrick Carey
  • Beginning XML by Hunter et. al.
  • Any book in the Sam's Teach Yourself XML series

Many of these may be found in the library. Library systems usually offer free access to databases such as Safari Books Online, which carries complete versions of some XML texts.

Resources

  • Tim Bray, who wrote the 1998 XML specifications and is of Sun Microsystems, has a blog where he expresses his dissatisfactions, hopes, and dreams for XML. Type "XML" into the search box to pull up relevant articles


 
blog comments powered by Disqus
Email to a friend