_chapter 12.htm

来自「Core Java 2(中文名称:JAVA 2 核心技术 卷二:高级特性)这是英」· HTM 代码 · 共 360 行 · 第 1/2 页

HTM
360
字号
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Chapter 12</title>
<link rel="stylesheet" type="text/css" href="docsafari.css">
<link rel="stylesheet" type="text/css" href="style.css">

</head>

<body>
<ul></ul>
<table width="100%" border="1" bgcolor="#EBEBFF">
  <tr>
    <td width="5%" align="left" valign="middle"><a href="_chapter%2011.htm"><img src="Larrow.gif" width="17" height="19" border="0"></a></td>
    <td align="center" valign="middle"><a class="docLink" href="Front%20matter.htm">CONTENTS</a></td>
    <td width="5%" align="right" valign="middle"><img src="Rarrow.gif" width="17" height="19" border="0"></td>
  </tr>
</table>

<h2 class="docChapterTitle">Chapter 12. XML</h2>
<ul>
  <li>
    <p class="docList"><a class="docLink" href="#c12s1">An Introduction to XML</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c12s2">Parsing an XML Document</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c12s3">Document Type Definitions</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c12s4">Namespaces</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c12s5">Using the SAX Parser</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c12s6">Generating XML Documents</a></li>
  <li> 
    <p class="docList"><a class="docLink" href="#c12s7">XSL Transformations</a></li>
</ul>
<p class="docText">The preface of the book <i>Essential XML</i> by Don Box, et 
al. [Addison-Wesley 2000] states only half-jokingly: &quot;The Extensible Markup 
Language (XML) has replaced Java, Design Patterns, and Object Technology as the 
software industry's solution to world hunger.&quot; Indeed, as you will see in this 
chapter, XML is a very useful technology for describing structured information. 
XML tools make it easy to process and transform that information. But XML is not 
a silver bullet. You need domain-specific standards and code libraries to use it 
effectively. Moreover, far from making Java obsolete, XML and Java work very 
well together. Essentially all important XML libraries have been implemented 
first in Java, and many of them are unavailable in any other programming 
language. Since the late 1990s, IBM, Apache, and others have been instrumental 
in producing high-quality Java libraries for XML processing. Starting with SDK 
1.4, Sun has integrated the most important libraries into the Java 2 Platform, 
Standard Edition.</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">You can download the Java API for XML Processing (JAXP) 
      library from
      <a class="docLink" href="http://java.sun.com/xml" target="_blank">
      http://java.sun.com/xml</a> to add the same XML processing capabilities to 
      older Java installations.</td>
    </tr>
  </table>
</div>
<p class="docText">This chapter gives an introduction into XML and covers the 
XML features of SDK 1.4. As always, we will point out along the way when the 
hype surrounding XML is justified, and when you have to take it with a grain of 
salt and solve your problems the old-fashioned way, through good design and 
code.</p>
<h3 class="docSection1Title" id="c12s1">An Introduction to XML</h3>
<p class="docText">You have seen several examples of <span class="docEmphasis">
property files</span> to describe the configuration of a program, for example in
<a class="docLink" href="_chapter%202.htm">Chapters 2</a> and
<a class="docLink" href="_chapter%204.htm">4</a>. A property file 
contains a set of name/value pairs, such as</p>
<pre>fontname=Times Roman
fontsize=12
windowsize=400 200
color=0 50 100
</pre>
<p class="docText">You can use the <tt>Properties</tt> class to read in such a 
file with a single method call. That's a nice feature, but it doesn't really go 
far enough. In many cases, the information that you want to describe has more 
structure than the property file format can comfortably handle. For example, 
consider the <tt>fontname/fontsize</tt> entries in the example. It would be more 
object-oriented to have a single entry:</p>
<pre>font=Times Roman 12
</pre>
<p class="docText">But then parsing the font description gets ugly梱ou have to 
figure out when the font name ends and when the font size starts.</p>
<p class="docText">Property files have a single flat hierarchy. You can often 
see programmers work around that limitation with key names such as</p>
<pre>title.fontname=Helvetica
title.fontsize=36
body.fontname=Times Roman
body.fontsize=12
</pre>
<p class="docText">Another shortcoming of the property file format is caused by 
the fact that keys have to be unique. To store a sequence of values, you need 
another workaround, such as</p>
<pre>menu.item.1=Times Roman
menu.item.2=Helvetica
menu.item.3=Goudy Old Style
</pre>
<p class="docText">The XML format solves these problems because it can express 
hierarchical structures, which is more flexible than the flat table structure of 
a property file.</p>
<p class="docText">An XML file for describing a program configuration might look 
like this:</p>
<pre>&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;body&gt;
         &lt;name&gt;Times Roman&lt;/name&gt;
         &lt;size&gt;12&lt;/size&gt;
   &lt;/body&gt;
   &lt;window&gt;
      &lt;width&gt;400&lt;/width&gt;
      &lt;height&gt;200&lt;/height&gt;
   &lt;/window&gt;
   &lt;color&gt;
      &lt;red&gt;0&lt;/red&gt;
      &lt;green&gt;50&lt;/green&gt;
      &lt;blue&gt;100&lt;/blue&gt;
   &lt;/color&gt;
   &lt;menu&gt;
      &lt;item&gt;Times Roman&lt;/item&gt;
      &lt;item&gt;Helvetica&lt;/item&gt;
      &lt;item&gt;Goudy Old Style&lt;/item&gt;
   &lt;/menu&gt;
&lt;/configuration&gt;
</pre>
<p class="docText">The XML format allows you to express the structure hierarchy 
and repeated elements without contortions.</p>
<p class="docText">As you can see, the format of an XML file is very 
straightforward. It looks very similar to an HTML file. There is a good 
reason梑oth the XML and HTML formats are descendants of the venerable Standard 
Generalized Markup Language (SGML).</p>
<p class="docText">SGML has been around since the 1970s for describing the 
structure of complex documents. It has been used with good success in some 
industries that require ongoing maintenance of massive documentation, in 
particular the aircraft industry. But SGML is quite complex, so it has never 
caught on in a big way. Much of that complexity arises from the fact that SGML 
has two conflicting goals. SGML wants to make sure that documents are formed 
according to the rules for their document type. But it also wants to make data 
entry easy by allowing shortcuts that reduce typing. XML was designed as a 
simplified version of SGML for use on the Internet. As is often true, simpler is 
better, and XML has enjoyed the immediate and enthusiastic reception that has 
eluded SGML for so long.</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">You can find a very nice version of the XML standard, 
      with annotations by Tim Bray, at
      <a class="docLink" href="http://www.xml.com/axml/axml.html" target="_blank">
      http://www.xml.com/axml/axml.html</a>.</td>
    </tr>
  </table>
</div>
<p class="docText">Even though XML and HTML have common roots, there are 
important differences between the two.</p>
<ul>
  <li>
  <p class="docList">Unlike HTML, XML is case sensitive. For example, <tt>&lt;H1&gt;</tt> 
  and <tt>&lt;h1&gt;</tt> are different XML tags.</li>
  <li>
  <p class="docList">In HTML, you can omit end tags such as <tt>&lt;/p&gt;</tt> or <tt>
  &lt;/li&gt;</tt> tags if it is clear from the context where a paragraph or list item 

⌨️ 快捷键说明

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