Accessibility

Dreamweaver Article

 

XML Overview


Table of Contents

Comments

What is XML?

By now, you probably know that XML stands for EXtensible Markup Language. OK, so it's a markup language, much like HTML, which means it uses tags. But what does it do, and why do your clients ask for it? Surprisingly, XML doesn't do anything. It simply describes information and distributes it in a platform-independent format.

XML achieves platform-independence by not using a specific language. XML tags are not predefined, which means you get to write your own tags. The advantage that comes with this is that XML is self-descriptive.

Believe it or not, you've been using XML all along, without even noticing it. When you read news headlines directly in your e-mail client, or when you visit web pages from your cell-phone, you're actually using XML-based technology.

OK, XML is Like HTML. Why Should I Use XML Instead of HTML?

First of all, XML is not a replacement for HTML and it has a totally different purpose. XML was designed to describe, store, and exchange data, while HTML was designed to present data in a human-readable format. HTML uses a fixed set of predefined elements (usually known as tags and attributes) to define visual aspects of a document, such as page layout, text formatting, and to include links to documents or images. In HTML, you are confined to using this limited set of tags, therefore the type of information you can display is limited. Displaying a mathematical formula with HTML, for instance, can cause you a lot of hassle. XML solves such problems through extensibility: you can "invent" your own tags and your own document structure. You can add and remove elements without affecting the overall structure of the document.

For instance, the following can be an XML description for a company department:

<department>
  <employee>
    	<name>John Doe</name>
	<job>Software Analyst</job>
    	<salary>2000</salary>
  </employee>
  <employee>
    	<name>Jane Fletcher</name>
    	<job>Designer</job>
	<salary>2500</salary>
  </employee>
</department>

You can try typing or copying this text into a new XML document in Dreamweaver, and then preview it in a browser. To create a new XML document in Dreamweaver MX 2004, click the File menu and select New. In the New Document window, select the Basic page category, then select XML from the list of basic pages.

Figure 1. Creating an XML document in the New Document dialog box

Figure 1. Creating an XML document in the New Document dialog box

Then, click the Create button. Dreamweaver will create a document that contains a line similar to this:

<?xml version="1.0" encoding="iso-8859-1"?>

This is the XML declaration and it must be included at the beginning of each XML document. It specifies the XML version and the character set used in the document.

Type the text from the above example in the new XML document after the first line. Notice that Dreamweaver natively supports syntax coloring for XML documents:

Figure 2. XML syntax code coloring support

Figure 2. XML syntax code coloring support

Save your first XML document and preview it in the browser. You'll notice that your browser will also color the code and will display the XML document as a collapsible tree (if your browser has XML support).

Figure 3. A collapsible XML tree.

Figure 3. A collapsible XML tree.

Note: Most browsers have XML support by default. To see how different browsers display and handle XML documents, please visit this page of the W3Schools website.

Clicking the minus sign next to each tag will collapse the element. To expand an element, you'll have to click the plus sign next to it.

Figure 4. The XML tree can be expanded and collapsed by clicking the plus and minus signs.

Figure 4. The XML tree can be expanded and collapsed by clicking the plus and minus signs.

The same example would be written in HTML like this:

<table>
	<tr>
		<td>John Doe</td>
		<td>Software Analyst</td>
		<td>2000</td>
	</tr>
	<tr>
		<td>Jane Fletcher</td>
		<td>Designer</td>
		<td>2500</td>
	</tr>
</table>

If you load the page in the browser, it will look like a classic HTML table ( I added a border for visibility).

Figure 5. The same content in HTML presentation markup

Figure 5. The same content in HTML presentation markup

The tags in the example above were specifically designed to describe information about a company's employees. By comparing the two examples, you can clearly see that XML is content-driven, while HTML is format-driven—the XML tag names describe the data in the tags, whereas the HTML tags describe the presentation of the data in the tags.

The example above is meant to illustrate some key differences between XML and HTML. However, you should be aware that XML was not designed as a replacement for HTML and not every XML document can be translated into an HTML document.

OK, So XML Is Different From HTML. Then What Is XHTML?

At this point, things might seem a bit blurry with all these new buzz words floating around you. While XHTML is beyond the scope of this article, I will briefly explain what it is, to make things more clear.

XHTML stands for Extensible Hyper Text Markup Language and it's a cleaner, XML-based version of HTML. It is a stricter specification of HTML, designed to eventually replace HTML. XHTML contains almost the same tags as HTML, but rewritten to observe the XML syntax rules, which will be presented in greater detail further in this article.

If XML Stores Data, is XML a Database?

Since XML stores and describes data, it resembles a database. You've probably already heard the words "schema," or "query language" mentioned in relation to XML. What makes then XML different from a typical DBMS (database management system)?

First of all, XML is portable. While databases rely on the specific database language they were designed for in order to be correctly interpreted, XML carries its meaning in its tags. XML is self-describing, which means it carries both the structure and the semantics of the data, while databases can only define the structure of data.

Furthermore, XML can represent data as hierarchical trees, as you have seen in the previous section. For instance, the <employee> element is a child of the <department> element, and is embedded as such in the XML tree.

Of course XML also has its drawbacks when compared to databases, but this is normal, since they were designed with different goals in mind. The most obvious disadvantage is that XML lacks database-specific features such as triggers, multi-user access, efficient storage, indexes, security, transactions, data integrity checks, or queries across multiple documents. This is normal, considering that a DBMS is designed for manipulating, storing, and retrieving data in a fast and secure way, while XML was designed to exchange data across platforms.

Consequently, searching in XML documents is also slower, because of the lack of indexes and search optimization features, common to databases.

Also, XML is more verbose, requiring a pair of tags and/or attributes for each data item.

While the data-centric/document-centric divide is somewhat obsolete, it is nevertheless important for understanding the XML philosophy and the main concepts behind the XML technology.

The next section looks at some of the advantages and disadvantages of XML and illustrates some real-life situations where you would be better off by using XML.