XML Introduction
Life before XML | What is XML? | Why is XML good? | What's wrong with XML? | Where is XML going? | Summary | Related information
XML (eXtensible Markup Language) is a new document format that is
generating an enormous amount of excitement in the Internet community.
It was originally conceived as a better way of creating Web pages,
similar to HTML except more powerful and flexible. However, in
reality Web pages are not the big story for XML, at least not in
the near future. XML's real impact is in the area of data
interchange and integration. Over the next few years XML promises to
revolutionize the way that applications and enterprises exchange
information. XML makes it much easier for applications to work
together, even when they are in different organizations.
In the past it was difficult to pass information between
applications. To begin with, each application represented its
data in a different
format. Many applications allowed other applications to access their
data using network protocols or procedural APIs, but the mechanism
was different for every application. Application data was typically
stored in a binary form with no obvious structure: knowledge of what
the data meant was buried in the code of the application that read
and wrote the data. For example, an application might have represented
data about an employee in the following binary form:
In this example the first ten bytes of the employee record contain the
employee's name as ASCII characters; the next byte contains the
employee's age in years as an integer binary value; and the last seven bytes
contain the employee's phone number as ASCII characters. In order
for an application to use the data, it must know what the
fields are, how large each one is, and how it is represented; this
information can't be determined just by looking at the data in the
record. If you wanted some other application to be able to use this
data, you would first go to the application that created the employee
record, search through its code or documentation to find out the
format of the employee record, then write special-purpose code for the
other application to access the data. The code written for
this data would not be useful for reading information written by any
other application, since it would have a different structure.
To make matters worse, the binary form used for data often depends on
the computer where the data was generated: an application running on
a Windows desktop might generate a different format than the same
application running on a Unix server. This makes data interchange
even more difficult, since you have to know what kind of computer
was used to generate the data (and this isn't obvious
from the data itself).
Finally, pre-XML data representations tend to be brittle. The programs
that read and write the data expect a single precise format such as the
one shown above. If the format changes in any way, existing applications
will no longer be able to read or write the data properly.
For example, if the name field were lengthened from ten characters
to twelve, or if a new salary field were added between the age and the
phone number, code changes would have to be made in every application
that uses the data.
XML is a specification for how to organize structured data into
documents. It is simple and yet remarkably flexible
and powerful. There are two basic ideas behind XML. First,
an XML document is just text: there is no binary data in an XML
document. Second, an XML document contains tags
that describe the structure of the information in the document.
Here is an XML document that represents the same information
as the binary form shown above:
<employee>
<name>John Doe</name>
<age>34</age>
<phone>211-1122</phone>
</employee>
Tags are the constructs such as <phone>. They come
in pairs with a tag such as <phone> marking the start
of a particular piece of information and a matching tag such
as </phone> marking the end of the information. A pair
of tags along with all of the tags and text they enclose is called
an element. This
document contains four elements. Three elements contain
individual fields such as name or age and the fourth element,
marked by <employee> and </employee>,
constitutes the entire document.
XML elements can be nested to represent structured information. In the
example above, the nesting of the elements shows that each
employee has three subfields: name,
age, and phone. Elements can be nested
to arbitrary depth. For example, the following XML document
describes a company with two employees:
<company>
<employee>
<name>John Doe</name>
<age>34</age>
<phone>211-1122</phone>
</employee>
<employee>
<name>Jane Smith</name>
<age>28</age>
<phone>211-1123</phone>
</employee>
</company>
Different XML documents can use different element names in different
hierarchical arrangements. In order to be a proper XML
document only a few simple rules must be followed, such as
having an outer element that contains the entire document
and nesting elements in a strict hierarchical relationship.
For example, the following document is not proper XML because
the <c> element is not properly nested
inside the <b> element:
<a><b><c></b></c></a>
In order to constitute well-formed XML, the document must have a
properly nested structure such as the following:
<a><b><c></c></b></a>
Although in principle every XML document can have a unique structure,
it is often convenient to insist that certain documents have a
particular arrangement of elements. XML supports the notion of a
Document Type Definition (DTD), which specifies a particular
arrangement of elements within an XML document. For example, the
DTD for a company database might specify
that each company database consists of a company element containing
one or more employee elements, and that each employee element contains
a name, an age, and a phone number in exactly that order. An XML
document is considered valid if it conforms to a particular DTD,
and an XML-based application can validate a document to ensure that
it has an appropriate structure before using it.
XML offers many advantages as a general-purpose mechanism for representing
data and communicating between applications:
Flexibility. XML can be used for an enormous variety of
different purposes just by defining element names and arrangements
appropriate for the particular purpose. For example, a database
record might be represented with an element for each field in the
record, using the field name from the database as the element
name in XML. Or, a message requesting an invocation of an
API function might be represented in XML with an element for the
name of the procedure and an additional element for each argument,
with the argument name used as the XML element's name. Since
each element is clearly marked with begin and end tags, elements
can grow and shrink as needed. In the employee record example above,
the binary representation reserves ten bytes for the employee's name,
so it cannot handle names longer than ten characters; the XML
representation can handle arbitrary lengths. Finally, the
nesting property of XML elements makes it easy to combine smaller
documents into larger documents.
Portability. XML documents can be moved easily among machines
or over the Internet because they are based on text, not binary
representations. Thus they aren't tied to the binary formats of any
particular type of computer. The text form used in XML is Unicode,
which supports all of the world's languages, so it is easy to use XML
for applications that span national boundaries.
Self-describing. Each XML document carries a structural
description of its contents with it in the form of the element
tags. This makes it much easier for one application to use an XML
document created by a different, unknown, application.
General-purpose tools. Since all XML documents have the same
basic form, general-purpose tools can be created that
operate on any XML document, such as tools to create documents,
display their contents, modify their structure, or record statistics
about the flow of XML documents in a system.
Several general-purpose XML parsers have already been created, which
make it easy to XML-enable applications.
Robustness. Because XML documents are self-describing, XML-based
applications can tolerate errors and evolve easily.
Consider the employee example from above, and suppose that the
employee's age were omitted, so that the telephone number followed
immediately after the name. In the binary representation the first
byte of the telephone number would be used incorrectly as the
age, since the reader has no way of knowing that the age was omitted.
In the XML representation, the absence of the age field would be
recognizable by the absence of tags. The reader could choose
to continue without the age information, if that makes sense,
or it could at least generate a
sensible error message describing what is missing. The tags also
allow graceful evolution of XML-based software. A new element can
be added to a document without affecting existing software that
uses the document: old
software will simply ignore the new element. Over time, existing
software can gradually be updated to take advantage of the new
information. Binary formats are much more brittle: any change to
the structure requires every reader and writer to be updated
immediately; if not, they are likely to misinterpret the data.
Human-readability. Although XML is intended for processing
by computer programs, its textual form is also relatively easy for
humans to read. This can be useful when debugging XML-based
applications and means that, in desperate situations, a human can use
an ordinary text editor to create or repair XML documents.
XML has three disadvantages with respect to binary representations,
all of which are inevitable consequences of XML's flexibility:
Size. XML documents occupy more space than binary representations
due to the use of text for everything and the presence of the tags.
Thus, XML documents will take more space on disk and may also take
more time when transmitting over a network.
Performance. It takes more work to read and write XML
documents than binary formats. The tags must be read and processed,
and information such as numbers will have to be converted from its
textual form to the binary form that the application needs. In
contrast, everything in a binary representation is in a well-defined
place in a ready-to-use form. The fixed format of a
binary representation has many disadvantages, which were discussed
above, but it does provide better performance.
Complexity. Reading an XML document is more complicated than
reading a fixed-format binary document due to the tag processing that
must occur. Thus it will be more difficult to create an XML parser
than a parser for a particular binary format. However, an XML parser
can be reused for many different applications. Furthermore, there
are many freely available XML parsers so you shouldn't need to
write a parser yourself.
Fortunately, the advantages of XML outweigh its disadvantages for
almost all imaginable purposes. Disk space is cheap and
getting cheaper rapidly, so XML's additional storage needs should
not be a major problem. Network bandwidth is also becoming
cheaper over time. Today's computers are already fast enough
to process XML documents quickly, so XML's performance
disadvantage won't matter except for applications with
very high transaction rates. Over time, faster and faster processors
will make performance even less of an issue. Finally,
the complexity of XML parsers shouldn't be a large factor because
there are free XML parsers readily available.
XML is a relatively young technology that is just beginning to
see significant usage. The first applications of XML are
in areas where integration problems are most
complex and XML's flexibility offers the greatest advantage.
The most promising area for early adoption of XML is business-to-business
commerce and other communication between enterprises. This area
presents particularly complicated problems because no one enterprise
has control over the applications and data formats of another
enterprise. Furthermore, each enterprise evolves its own resources
independently. XML has the flexibility to accommodate the diverse
resources required for inter-enterprise communication, and it
makes it easier to evolve business-to-business processes over time.
Business-to-business applications are implemented with XML-based
servers that transmit XML documents between enterprises using Web-based
protocols such as HTTP. These B2B integration servers provide
interfaces to existing corporate resources such
as databases and ERP applications.
Humans running the applications or browsing
Web sites trigger the creation and transmission
of XML documents, and the receiving B2B integration server passes information
from incoming XML documents to other applications in the enterprise.
Business processes are described in terms of rules for creating
and handling XML documents. Several companies, such as Scriptics,
are creating B2B integration servers based on XML.
Once XML has gained maturity through its use in the business-to-business
arena it will also come to be used for intra-enterprise applications such
as communication between departments and divisions. Eventually XML
will spread throughout the enterprise for integration applications
large and small. Most of the problems currently being addressed by Enterprise
Application Integration (EAI) vendors will eventually involve heavy
usage of XML.
Although XML will serve many purposes in the years ahead, its most
important and mission-critical uses will have to do with data
interchange and application integration. XML offers the flexibility
to represent many different kinds of data in a single form.
The use of tags to mark the structure of XML document is the source
of its flexibility, and also allows XML-based applications to
evolve easily.