Java Reference
In-Depth Information
Don't forget that there must be only one root element that encloses all the other elements in a document.
This implies that you cannot have an element of the same type as the root element as a child of any element
in the document.
Empty Elements
You already know that an element can contain nothing at all, so just a start tag immediately followed by an
end tag is an empty element . For example:
<commercial></commercial>
You have an alternative way to represent empty elements. Instead of writing a start and an end tag with
nothing between them, you can write an empty element as a single tag with a forward slash immediately
following the tag name:
<commercial/>
This is equivalent to a start tag followed by an end tag. There must be no spaces between the opening <
and the element name, or between the / and the > marking the end of the tag.
You may be thinking at this point that an empty element is of rather limited use, whichever way you
write it. Although by definition an empty element has no content, it can and often does contain additional in-
formation that is provided within attributes that appear within the tag. You see how you add attributes to an
element a little later in this chapter. An empty element can be used as a marker or flag to indicate something
about the data within its parent. For example, you might use an empty element as part of the content for
an <address> element to indicate that the address corresponds to a commercial property. Absence of the
<commercial/> element indicates a private residence.
Document Comments
When you create an XML document using an editor, it is often useful to add explanatory text to the docu-
ment. You can include comments in an XML document like this:
<!-- Prepared on 14th January 2011 -->
Comments can go just about anywhere in the prolog or the document body, but not inside a start tag or an
end tag, or within an empty element tag. You can spread a comment over several lines if you wish, like this:
<!--
Eeyore, who is a friend of mine,
has lost his tail.
-->
For compatibility with SGML, from which XML is derived, the text within a comment should not contain
a sequence of two or more hyphens, and it must not end with a hyphen. A comment that ends with ---> is
not well-formed and is rejected by an XML processor. Although an XML processor of necessity scans com-
ments to distinguish them from markup and document data, they are not part of the character data within a
document. XML processors need not make comments available to an application, although some may do so.
Search WWH ::




Custom Search