_chapter 12.htm
来自「Core Java 2(中文名称:JAVA 2 核心技术 卷二:高级特性)这是英」· HTM 代码 · 共 360 行 · 第 1/2 页
HTM
360 行
ends. In XML, you can never omit an end tag.</li>
<li>
<p class="docList">In XML, elements that have a single tag without a matching
end tag must end in a <tt>/</tt>, such as <tt><img src="coffeecup.png"/></tt>.
That way, the parser knows not to look for a <tt></img></tt> tag.</li>
<li>
<p class="docList">In XML, attribute values must be enclosed in quotation
marks. In HTML, quotation marks are optional. For example, <tt><applet code="MyApplet.class"
width=</tt><span class="docEmphStrong"><tt>300</tt></span> <tt>height=</tt><span class="docEmphStrong"><tt>300</tt></span><tt>></tt>
is legal HTML but not legal XML. In XML, you would have to use quotation
marks: <tt>width=</tt><span class="docEmphStrong"><tt>"</tt></span><tt>300</tt><span class="docEmphStrong"><tt>"</tt></span>.</li>
<li>
<p class="docList">In HTML, you can have attribute names without values, such
as <tt><input type="radio" name="language" value="Java"</tt>
<span class="docEmphStrong"><tt>checked</tt></span><tt>></tt>. In XML, all
attributes must have values, such as <tt>checked="true"</tt> or (ugh) <tt>
checked="checked"</tt>.</li>
</ul>
<div class="docNote">
<p class="docNoteTitle">NOTE</p>
<table cellSpacing="0" cellPadding="1" width="90%" border="0">
<tr>
<td vAlign="top" width="60">
<img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
</td>
<td vAlign="top">
<p class="docText">The current recommendation for web documents by the
World Wide Web Consortium (W3C) is the XHTML standard, which tightens up
the HTML standard to be XML compliant. You can find a copy of the XHTML
standard at
<a class="docLink" href="http://www.w3.org/tr/xhtml1/" target="_blank">
http://www.w3.org/TR/xhtml1/</a>. XHTML is backwards-compatible with
current browsers, but unfortunately many current HTML authoring tools do
not yet support it. Once XHTML becomes more widespread, you can use the
XML tools that are described in this chapter to analyze web documents.</td>
</tr>
</table>
</div>
<h4 class="docSection2Title" id="ch12lev2sec1">The Structure of an XML Document</h4>
<p class="docText">An XML document should start with a header such as</p>
<pre><?xml version="1.0"?>
</pre>
<p class="docText">or</p>
<pre><?xml version="1.0" encoding="UTF-8"?>
</pre>
<p class="docText">Strictly speaking, a header is optional, but it is highly
recommended.</p>
<div class="docNote">
<p class="docNoteTitle">NOTE</p>
<table cellSpacing="0" cellPadding="1" width="90%" border="0">
<tr>
<td vAlign="top" width="60">
<img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
</td>
<td vAlign="top">
<p class="docText">Because SGML was created for processing of real
documents, XML files are called <span class="docEmphasis">documents,</span>
even though most XML files describe data sets that one would not normally
call documents.</td>
</tr>
</table>
</div>
<p class="docText">The header is normally followed by a
<span class="docEmphasis">document type declaration,</span> such as</p>
<pre><!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
</pre>
<p class="docText">Document type declarations are an important mechanism to
ensure the correctness of a document, but they are not required. We will discuss
them later in this chapter.</p>
<p class="docText">Finally, the body of the XML document contains the
<span class="docEmphasis">root element,</span> which can contain other elements.
For example,</p>
<pre><?xml version="1.0"?>
<!DOCTYPE configuration . . .>
<configuration>
<title>
<font>
<name>Helvetica</name>
<size>36</size>
</font>
</title>
. . .
</configuration>
</pre>
<p class="docText">An element can contain <span class="docEmphasis">child
elements,</span> text, or both. In the example above, the <tt>font</tt> element
has two child elements, <tt>name</tt> and <tt>size</tt>. The <tt>name</tt>
element contains the text <tt>"Helvetica"</tt>.</p>
<div class="docNote">
<p class="docNoteTitle">TIP</p>
<table cellSpacing="0" cellPadding="1" width="90%" border="0">
<tr>
<td vAlign="top" width="60">
<img alt="graphics/tip.gif" src="tip.gif" align="left" border="0" width="54" height="50"><br>
</td>
<td vAlign="top">
<p class="docText">It is best if you structure your XML documents such
that an element contains <span class="docEmphasis">either</span> child
elements <span class="docEmphasis">or</span> text. In other words, you
should avoid situations such as</p>
<pre><font>
Helvetica
<size>36</size>
</font>
</pre>
<p class="docText">This is called <span class="docEmphasis">mixed contents</span>
in the XML specification. As you will see later in this chapter, you can
design much cleaner document type definitions if you avoid mixed contents.</td>
</tr>
</table>
</div>
<p class="docText">XML elements can contain attributes, such as</p>
<pre><size unit="pt">36</size>
</pre>
<p class="docText">There is some disagreement among XML designers about when to
use elements and when to use attributes. For example, it would seem easier to
describe a font as</p>
<pre><font name="Helvetica" size="36"/>
</pre>
<p class="docText">than</p>
<pre><font>
<name>Helvetica</name>
<size>36</size>
</font>
</pre>
<p class="docText">However, attributes are much less flexible. Suppose you want
to add units to the size value. If you use attributes, then you have to add the
unit to the attribute value:</p>
<pre><font name="Helvetica" size="36 pt"/>
</pre>
<p class="docText">Ugh! Now you have to parse the string <tt>"36 pt"</tt>, just
the kind of hassle that XML was designed to avoid. Adding an attribute to the
<tt>size</tt> element is much cleaner:</p>
<pre><font>
<name>Helvetica</name>
<size unit="pt">36</size>
</font>
</pre>
<p class="docText">A commonly used rule of thumb is that attributes should only
be used when they modify the interpretation of a value, not to specify values.
If you find yourself engaged in metaphysical discussions about whether a
particular setting is a modification of the interpretation of a value or not,
then just say "no" to attributes and use elements throughout. Many useful DTDs
don't use attributes at all.</p>
<div class="docNote">
<p class="docNoteTitle">NOTE</p>
<table cellSpacing="0" cellPadding="1" width="90%" border="0">
<tr>
<td vAlign="top" width="60">
<img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
</td>
<td vAlign="top">
<p class="docText">In HTML, the rule for attribute usage is simple: If it
isn't displayed on the web page, it's an attribute. For example, consider
a hyperlink</p>
<pre><a href="http://java.sun.com">Java Technology</a>
</pre>
<p class="docText">The string <tt>Java Technology</tt> is displayed on the
web page, but the URL of the link is not a part of the displayed page.
However, that rule isn't all that helpful for most XML files since the
data in an XML file isn't normally meant to be viewed by humans.</td>
</tr>
</table>
</div>
<p class="docText">Elements and text are the "bread and butter" of XML
documents. There are a few other markup instructions that you may encounter.</p>
<ul>
<li>
<p class="docList"><span class="docEmphasis">Character references</span> have
the form <tt>&#</tt><span class="docEmphasis"><tt>d</tt></span><tt>;</tt> or
<tt>&#x</tt><span class="docEmphasis"><tt>h</tt></span><tt>;</tt>. Here
<span class="docEmphasis">d</span> is a decimal Unicode value and
<span class="docEmphasis">h</span> is a hexadecimal Unicode value. Examples
are</p>
<pre>&#233;
&#x2122;
</pre>
<p class="docList">which denote the characters
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?