Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

S E S S I O N

6

XML: A Primer

Session Checklist

Understanding the basics and promise of XML

Learning to create a simple XML document

You have probably heard a great deal about eXtensible Markup Language (XML) over the past few years. XML is on its way to becoming the de facto language for communications between devices, Web browsers, computers, servers, and applications. In time, any two

applications will be able to exchange information without ever having been designed to talk to each other.

In many ways, XML is just another file format — one more way to store information. However, XML as a file format is just the beginning. XML promises to liberate information from proprietary file formats and make it possible for information to move among multiple programs on different types of computers without facing the battery of conversion programs and lost information that is currently necessary. XML promises to dramatically increase both the efficiency and flexibility of the ways in which you handle information. In doing so, XML will have an impact on the way in which you use computers; it will change the way you look at applications.

Fundamentally, XML makes it easy to store information in a hierarchical format, providing a consistent, easy-to-parse syntax and a set of tools for building rules describing the structure used to contain information. The XML format can represent both simple and complex information, and allows developers to create their own vocabularies for describing that information. XML documents can describe both themselves and their content.

The XML Design Specs

When you think of an “application,” you tend to think of a Web application — or a desktop application like Word or Excel. However, the creators of XML were a little less nearsighted

56

Saturday Morning

when they developed the XML specification. They saw XML as a way of sharing data among many different kinds of applications. For this reason, the creators of XML established the following design commandments for the XML specification:

1.XML shall be straightforwardly usable over the Internet.

This does not mean that XML should only be used over the Internet, but rather that it should be lightweight and easily usable over the Internet.

2.XML shall support a wide variety of applications.

The idea here is that XML should not be application specific. It can be used over the Internet or in a traditional client/server application. There is no specific technology behind XML, so any technology should be able to use it.

3.It shall be easy to write programs that process XML documents.

Unable to gain wide acceptance for various reasons, many technologies come and go. A major barrier to wide acceptance is a high level of difficulty or complexity. The designers of XML wanted to ensure that it would gain rapid acceptance by making it easy for programmers to write XML parsers.

4.XML documents should be human-legible and reasonably clear.

Because XML is text-based and follows a strict but simple formatting methodology, it is extremely easy for a human to get a true sense of what a document means. XML is designed to describe the structure of its contents.

5.XML documents shall be easy to create.

XML documents can be created in a simple text-editor. Now that’s easy!

There are other XML guidelines, but since this only is an introduction to XML, these will do for now. The important thing to remember is that XML is simply a file format that can be used for two or more entities to exchange information.

XML documents are hierarchical: they have a single (root) element, which may contain other elements, which may in turn contain other elements, and so on. Documents typically look like a tree structure with branches growing out from the center and finally terminating at some point with content. Elements are often described as having parent and child relationships, in which the parent contains the child element.

The Structure of XML Documents

XML documents must be properly structured and follow strict syntax rules in order to work correctly. If a document is lacking in either if these areas, the document can’t be parsed. There are two types of structures in every XML document: logical and physical. The logical structure is the framework for the document and the physical structure is the actual data.

An XML document may consist of three logical parts: a prolog (optional), a document element, and an epilog (optional). The prolog is used to instruct the parser how to interpret the document element. The purpose of the epilog is to provide information pertaining to the preceding data. Listing 6-1 shows the basic structure of an XML document.

Session 6—XML: A Primer

57

Listing 6-1 Basic structure of an XML document

<?xml version=”1.0” ?>

<!-- Above is the prolog -->

<!-- The lines below are contained within the document element: BANDS --> <BANDS>

<BAND TYPE=”ROCK”>

<NAME>Hootie And The Blowfish</NAME> <MEMBERS>

<MEMBER> <FIRST_NAME>Darius</FIRST_NAME> <LAST_NAME>Rucker</LAST_NAME>

</MEMBER>

<MEMBER> <FIRST_NAME>Dean</FIRST_NAME> <LAST_NAME>Felber</LAST_NAME>

</MEMBER>

<MEMBER> <FIRST_NAME>Mark</FIRST_NAME> <LAST_NAME>Bryan</LAST_NAME>

</MEMBER>

<MEMBER> <FIRST_NAME>Jim</FIRST_NAME> <LAST_NAME>Sonefeld</LAST_NAME>

</MEMBER>

</MEMBERS>

<LABEL>Atlantic Recording Corporation</LABEL> </BAND>

</BANDS>

<!-- epilog goes here -->

The prolog is made up of two parts: the XML declaration and an optional Document Type Declaration (DTD). The XML declaration identifies the document as XML and lets the parser know that it complies with the XML specification. Although the prolog, and thereby the XML declaration, is optional, we recommend that you include them in all your XML documents. Here is an example of a simple XML declaration:

<?xml version=”1.0” ?>

The XML declaration can also contain more than just the version attribute. Some of the more important ones are the encoding and standalone attributes.

The document type declaration establishes the grammar rules for the document or it points to a document where these rules can be found. The DTD is optional, but, if included, must appear after the XML declaration.

XML documents can also reference a Schema rather than a DTD. Schemas perform essentially the same function as DTDs, but can describe more complex data types and are actually XML documents themselves. When possible, we recommend using a Schema rather than a DTD as Schemas are quickly becoming the de-facto standard for describing XML documents.

58

Saturday Morning

Note

An XML document is referred to as well formed when it conforms to all XML syntax rules. A valid XML document follows the structural rules defined in a Document Type Definition or Schema.

All the data in an XML document is contained within the document element (in this example, <BANDS>). You can’t have more than one document element in the same document, but the document element can contain as many child elements as necessary.

XML Syntax

The contents of an XML document are constructed using a very strict syntax that must conform to the following rules:

Tags are case sensitive.

All tags must be closed.

Attribute values must be enclosed in quotes.

Note

XML elements can have attributes that allow you to add information to an element that it does not contain. For example, in Listing 6-1, the BAND element has a TYPE attribute with a value of “ROCK”.

XML tags are very similar to HTML tags. The less-than (<) and greater-than (>) symbols are used to delimit tags and the forward slash (/) is used to indicate closing tags.

Elements are building blocks of an XML document. Every element in an XML document, with the exception of the document element, is a child element. Child elements can contain one of four content types:

Element content

Character content

Mixed content

Empty

In our example, the <BAND> and <MEMBERS> elements contain element content. All others contain character content.

All elements in an XML document are nested, which gives the document a hierarchical tree appearance. If you’ll notice in the <BANDS> example, all of elements’ sub-elements are indented. The rules for nesting are strictly enforced.

XML elements can also have attributes. For example:

<BAND TYPE=”ROCK”>

In the previous example, the <BAND> element has an attribute named TYPE that is used to indicate what kind of music the band plays. Notice that the attribute value is enclosed in