_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>&lt;img src=&quot;coffeecup.png&quot;/&gt;</tt>. 
  That way, the parser knows not to look for a <tt>&lt;/img&gt;</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>&lt;applet code=&quot;MyApplet.class&quot; 
  width=</tt><span class="docEmphStrong"><tt>300</tt></span> <tt>height=</tt><span class="docEmphStrong"><tt>300</tt></span><tt>&gt;</tt> 
  is legal HTML but not legal XML. In XML, you would have to use quotation 
  marks: <tt>width=</tt><span class="docEmphStrong"><tt>&quot;</tt></span><tt>300</tt><span class="docEmphStrong"><tt>&quot;</tt></span>.</li>
  <li>
  <p class="docList">In HTML, you can have attribute names without values, such 
  as <tt>&lt;input type=&quot;radio&quot; name=&quot;language&quot; value=&quot;Java&quot;</tt>
  <span class="docEmphStrong"><tt>checked</tt></span><tt>&gt;</tt>. In XML, all 
  attributes must have values, such as <tt>checked=&quot;true&quot;</tt> or (ugh) <tt>
  checked=&quot;checked&quot;</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>
&nbsp;</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>&lt;?xml version=&quot;1.0&quot;?&gt;
</pre>
<p class="docText">or</p>
<pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
</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>
&nbsp;</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>&lt;!DOCTYPE web-app PUBLIC
   &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN&quot;
   &quot;http://java.sun.com/j2ee/dtds/web-app_2_2.dtd&quot;&gt;
</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>&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;!DOCTYPE configuration . . .&gt;
&lt;configuration&gt;
   &lt;title&gt;
      &lt;font&gt;
         &lt;name&gt;Helvetica&lt;/name&gt;
         &lt;size&gt;36&lt;/size&gt;
      &lt;/font&gt;
   &lt;/title&gt;
   . . .
&lt;/configuration&gt;
</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>&quot;Helvetica&quot;</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>
&nbsp;</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>&lt;font&gt;
   Helvetica
   &lt;size&gt;36&lt;/size&gt;
&lt;/font&gt;
</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>&lt;size unit=&quot;pt&quot;&gt;36&lt;/size&gt;
</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>&lt;font name=&quot;Helvetica&quot; size=&quot;36&quot;/&gt;
</pre>
<p class="docText">than</p>
<pre>&lt;font&gt;
   &lt;name&gt;Helvetica&lt;/name&gt;
   &lt;size&gt;36&lt;/size&gt;
&lt;/font&gt;
</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>&lt;font name=&quot;Helvetica&quot; size=&quot;36 pt&quot;/&gt;
</pre>
<p class="docText">Ugh! Now you have to parse the string <tt>&quot;36 pt&quot;</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>&lt;font&gt;
   &lt;name&gt;Helvetica&lt;/name&gt;
   &lt;size unit=&quot;pt&quot;&gt;36&lt;/size&gt;
&lt;/font&gt;
</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 &quot;no&quot; 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>
&nbsp;</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>&lt;a href=&quot;http://java.sun.com&quot;&gt;Java Technology&lt;/a&gt;
</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 &quot;bread and butter&quot; 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>&amp;#</tt><span class="docEmphasis"><tt>d</tt></span><tt>;</tt> or
  <tt>&amp;#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>&amp;#233;
&amp;#x2122;
</pre>
  <p class="docList">which denote the characters 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?