The basic syntax of XML is extremely simple. If you’ve worked with
HTML, you’re already halfway there. As with HTML, XML represents
information as text using tags to add structure. A tag begins
with a name sandwiched between less than (<) and greater than (>)
characters. Unlike HTML, XML tags must always be
balanced; in other words, an opening tag must always
be followed by a closing tag. A closing tag looks just like the opening
tag but starts with a less than sign and a slash (</). An opening tag,
closing tag, and any content in between are collectively referred to as an
element of the XML document. Elements can contain
other elements, but they must be properly nested (all tags started within
an element must be closed before the element itself is closed). Elements
can also contain plain text or a mixture of elements and text (called
mixed content). Comments are enclosed between <!—
and —>
markers. Here are a few examples:
<!--
Simple
-->
<
Sentence
>
This
is
text
.</
Sentence
>
<!--
Element
-->
<
Paragraph
><
Sentence
>
This
is
text
.</
Sentence
></
Paragraph
>
<!--
Mixed
-->
<
Paragraph
>
<
Sentence
>
This
<
verb
>
is
</
verb
>
text
.</
Sentence
>
</
Paragraph
>
<!--
Empty
-->
<
PageBreak
></
PageBreak
>
An empty tag can be written more compactly in a special form using a single tag ending with a slash and a greater-than sign (/>):
<
PageBreak
/>
An XML element can contain attributes, which are simple name-value pairs supplied inside the start tag.
<
Document
type
=
"LEGAL"
id
=
"42"
>...</
Document
>
<
Image
name
=
"truffle.jpg"
/>
The attribute value must always be enclosed in quotes. You
can use double ("
) or single
('
) quotes. Single quotes are useful
if the value contains double quotes.
Attributes are intended to be used for simple, unstructured
properties or compact identifiers associated with the element data. It
is always possible to make an attribute into a child element, so,
strictly speaking, there is no real need for attributes. But they often
make the XML easier to read and more logical. In the case of the
Document
element in our preceding
snippet, the attributes type
and
ID
represent metadata about the
document. We might expect that a Java class representing the Document
would have an enumeration of document
types such as LEGAL
. In the case of
the Image
element, the attribute is
simply a more compact way of including the filename. As a rule,
attributes should be compact, with little significant internal structure
(URLs push the envelope); by contrast, child elements can have arbitrary
complexity.
The id
attribute in the
previous example may have special significance when used with a
corresponding idref
attribute.
Together, these standard attributes are used with document validation to
enforce referential integrity in documents. When validated, an id
attribute value must be unique within the
document and an idref
attribute value
must refer to a valid id
within the
document.
An XML document begins with a header like the following and one root element:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
MyDocument
>
</
MyDocument
>
The header identifies the version of XML and the character encoding used. The root element is simply the top of the element hierarchy, which can be considered a tree. If you omit this header or have XML text without a single root element (as in our earlier simple examples), technically what you have is called an XML fragment.
The default encoding for an XML document is UTF-8, the ASCII-friendly 8-bit Unicode encoding. This encoding preserves ASCII values, so English text is unaltered by it. It also allows Unicode values to be stored in a reasonably efficient way. An XML document may specify another encoding using the encoding attribute of the XML header.
Within an XML document, certain characters are necessarily
sacrosanct: for example, the <
and
>
characters that
indicate element tags. When you need to include these in your text, you
must encode them. XML provides an escape mechanism called “entities”
that allows for encoding special structures. XML has five predefined
entities, as shown in Table 24-1.
An alternative to encoding text in this way is to use a special
“unparsed” section of text called a character data (CDATA) section. A
CDATA section starts with the cryptic string <![CDATA[
and ends
with ]]>
, like this:
<![
CDATA
[
Learning
Java
,
O
'
Reilly
&
Associates
]]>
The CDATA section looks a little like a comment, but the data is still part of the document, just opaque to the parser.
There is one more alternative, which is to use a special
<include>
directive to include the contents of a URL or file either as pre-escaped
text or optionally parsed as XML. XML includes are very convenient, and
we’ll talk about them later in this chapter.
You’ve probably seen that HTML has a <body>
tag that is used to structure web
pages. Suppose for a moment that we are writing XML for a funeral home
that also uses the tag <body>
for some other, more macabre, purpose. This could be a problem if we
want to mix HTML with our mortuary information.
If you consider HTML and the funeral home tags to be languages in this case, the elements (tag names) used in a document are really the vocabulary of those languages. An XML namespace is a way of saying whose dictionary you are using for a given element, allowing us to mix them freely. (Later, we’ll talk about XML Schemas, which enforce the grammar and syntax of the language.)
A namespace is specified with the xmlns
attribute, whose
value is a Uniform Resource Identifier (URI) that uniquely defines
the set (and usually the meaning) of tags from that namespace:
<
element
xmlns
=
"namespaceURI"
>
Recall from Chapter 14 that a URI is not necessarily a URL. URIs are more general than URLs. In practical terms, a URI is to be treated as a unique string. Often, the URI is in fact also a URL for a document describing the namespace, but when true it is only by convention.
An xmlns
namespace attribute
can be applied to an element and affects all its (nested) children; this
is called a default namespace for the element:
<
body
xmlns
=
"http://funeral-procedures.org/"
>
Often it is desirable to mix and match namespaces on a tag-by-tag
basis. To do this, we can use the special xmlns
attribute to define a special identifier
for the namespace and use that identifier as a prefix on the tags in
question. For example:
<
funeral
xmlns:
fun
=
"http://funeral-procedures.org/"
>
<
html
><
head
></
head
><
body
>
<
fun:
body
>
Corpse
#
42
</
fun:
body
>
</
funeral
>
In the preceding snippet of XML, we’ve qualified the body tag with
the prefix “fun:”, which we defined in the <funeral>
tag. In this case, we should
qualify the root tag as well, reflexively:
<
fun:
funeral
xmlns:
fun
=
"http://funeral-procedures.org/"
>
The XML parser factories supplied with Java have a switch to specify whether you want the parser to interpret namespaces. This switch defaults to off for historical reasons.
parserFactory
.
setNamespaceAware
(
true
);
We’ll talk more about parsing in the sections on SAX and DOM later in this chapter.
A document that conforms to the basic rules of XML with
proper encoding and balanced tags is called a well-formed document. Just because a
document is syntactically correct, however, doesn’t mean that it makes
sense. Two related sets of tools, DTDs and XML Schemas, define ways to
provide a grammar for your XML elements. They allow you to create
syntactic rules, such as “a City
element can appear only once inside an Address
element and comes before a State
element.” XML Schema goes further to
provide a flexible language for describing the validity of data content
of the tags, including both simple and compound data types made of
numbers and strings.
A document that is checked against a DTD or XML Schema description and follows the rules is called a valid document. A document can be well formed without being valid, but not vice versa.
To speak very loosely, we could say that the most popular
and widely used form of XML in the world today is HTML. The terminology
is loose because HTML is not really well-formed XML. HTML tags violate
XML’s rule forbidding unbalanced elements; the common <p>
tag is
typically used without a closing tag, for example. HTML attributes also
don’t require quotes. XML tags are also case-sensitive; <P>
and <p>
are two different tags in XML. We
could generously say that HTML is “forgiving” with respect to details
like this, but as a developer, you know that sloppy syntax results in
ambiguity. XHTML is an alternate, strict XML version of HTML that is
clear and unambiguous. This form of HTML works in modern browsers.
Fortunately, if you want to switch, you don’t have to manually clean up
all your HTML documents; Tidy is an open source program
that automatically converts HTML to XHTML, validates it, and corrects
common mistakes.
Get Learning Java, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.