⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jaxpintro4.html

📁 j2eePDF格式的电子书
💻 HTML
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>The Simple API for XML (SAX) APIs</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="JAXPIntro3.html" />    <link rel="Next" href="JAXPIntro5.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="JAXPIntro3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXPIntro5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp64181"> </a><h2 class="pHeading1">The Simple API for XML (SAX) APIs</h2><a name="wp65754"> </a><p class="pBody">The basic outline of the SAX parsing APIs are shown at right. To start the process, an instance of the <code class="cCode">SAXParserFactory</code> class is used to generate an instance of the parser.</p><a name="wp65758"> </a><p class="pBody"></p><div align="left"><img src="images/Fig5-1.gif" height="225" width="311" alt="SAX APIs: SAXParserFactory creates a SAXParser, which reads XML data, sending events to a SAXReader, consisting of a Content Handller, Error Handler, DTD Handler, and Entity Resolver." border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="65759"> </a><strong><font >Figure 4-1    SAX APIs</font></strong></p><a name="wp64187"> </a><p class="pBody">The parser wraps a <code class="cCode">SAXReader</code> object. When the parser's <code class="cCode">parse()</code> method is invoked, the reader invokes one of several callback methods implemented in the application. Those methods are defined by the interfaces <code class="cCode">ContentHandler</code>, <code class="cCode">ErrorHandler</code>, <code class="cCode">DTDHandler</code>, and <code class="cCode">EntityResolver</code>. </p><a name="wp64188"> </a><p class="pBody">Here is a summary of the key SAX APIs: </p><a name="wp64190"> </a><p class="pDefinitionTerm"> <code class="cCode">SAXParserFactory</code></p><a name="wp64191"> </a><p class="pDefinition">A <code class="cCode">SAXParserFactory</code> object creates an instance of the parser determined by the system property, <code class="cCode">javax.xml.parsers.SAXParserFactory</code>.</p><a name="wp64193"> </a><p class="pDefinitionTerm"><code class="cCode">SAXParser</code></p><a name="wp64194"> </a><p class="pDefinition">The <code class="cCode">SAXParser</code> interface defines several kinds of <code class="cCode">parse()</code> methods. In general, you pass an XML data source and a <code class="cCode">DefaultHandler</code> object to the parser, which processes the XML and invokes the appropriate methods in the handler object.</p><a name="wp64196"> </a><p class="pDefinitionTerm"><code class="cCode">SAXReader</code></p><a name="wp64197"> </a><p class="pDefinition">The <code class="cCode">SAXParser</code> wraps a <code class="cCode">SAXReader</code>. Typically, you don't care about that, but every once in a while you need to get hold of it using <code class="cCode">SAXParser</code>'s <code class="cCode">getXMLReader()</code>, so you can configure it. It is the <code class="cCode">SAXReader</code> which carries on the conversation with the SAX event handlers you define.</p><a name="wp64199"> </a><p class="pDefinitionTerm"><code class="cCode">DefaultHandler</code></p><a name="wp64200"> </a><p class="pDefinition">Not shown in the diagram, a <code class="cCode">DefaultHandler</code> implements the <code class="cCode">ContentHandler</code>, <code class="cCode">ErrorHandler</code>, <code class="cCode">DTDHandler</code>, and <code class="cCode">EntityResolver</code> interfaces (with null methods), so you can override only the ones you're interested in. </p><a name="wp64202"> </a><p class="pDefinitionTerm"><code class="cCode">ContentHandler</code></p><a name="wp64203"> </a><p class="pDefinition">Methods like <code class="cCode">startDocument</code>, <code class="cCode">endDocument</code>, <code class="cCode">startElement</code>, and <code class="cCode">endElement</code> are invoked when an XML tag is recognized. This interface also defines methods <code class="cCode">characters</code> and <code class="cCode">processingInstruction</code>, which are invoked when the parser encounters the text in an XML element or an inline processing instruction, respectively.</p><a name="wp64205"> </a><p class="pDefinitionTerm"><code class="cCode">ErrorHandler</code></p><a name="wp64206"> </a><p class="pDefinition">Methods <code class="cCode">error</code>, <code class="cCode">fatalError</code>, and <code class="cCode">warning</code> are invoked in response to various parsing errors. The default error handler throws an exception for fatal errors and ignores other errors (including validation errors). That's one reason you need to know something about the SAX parser, even if you are using the DOM. Sometimes, the application may be able to recover from a validation error. Other times, it may need to generate an exception. To ensure the correct handling, you'll need to supply your own error handler to the parser.</p><a name="wp64208"> </a><p class="pDefinitionTerm"><code class="cCode">DTDHandler</code></p><a name="wp64209"> </a><p class="pDefinition">Defines methods you will generally never be called upon to use. Used when processing a DTD to recognize and act on declarations for an <span style="font-style: italic">unparsed entity</span>.</p><a name="wp64211"> </a><p class="pDefinitionTerm"><code class="cCode">EntityResolver</code></p><a name="wp64212"> </a><p class="pDefinition">The <code class="cCode">resolveEntity</code> method is invoked when the parser must identify data identified by a URI. In most cases, a URI is simply a URL, which specifies the location of a document, but in some cases the document may be identified by a URN--a <span style="font-style: italic">public identifier</span>, or name, that is unique in the Web space.   The public identifier may be specified in addition to the URL. The <code class="cCode">EntityResolver</code> can then use the public identifier instead of the URL to find the document, for example to access a local copy of the document if one exists.</p><a name="wp64213"> </a><p class="pBody">A typical application implements most of the <code class="cCode">ContentHandler</code> methods, at a minimum. Since the default implementations of the interfaces ignore all inputs except for fatal errors, a robust implementation may want to implement the <code class="cCode">ErrorHandler</code> methods, as well. </p><a name="wp64215"> </a><h3 class="pHeading2">The SAX Packages</h3><a name="wp64216"> </a><p class="pBody">The SAX parser is defined in the following packages listed in <a  href="JAXPIntro4.html#wp64222">Table 4-1</a>.</p><div align="left"><table border="1" summary="SAX Packages" id="wp64222">  <caption><a name="wp64222"> </a><div class="pTableTitle">Table 4-1   SAX Packages</div></caption>  <tr align="center">    <th><a name="wp65986"> </a><div class="pCellHeading">Package</div></th>    <th><a name="wp65988"> </a><div class="pCellHeading">Description</div></th></tr>  <tr align="left">    <td><a name="wp64230"> </a><div class="pCellBody"><code class="cCode"> org.xml.sax</code></div></td>    <td><a name="wp64232"> </a><div class="pCellBody">Defines the SAX interfaces. The name <code class="cCode">org.xml</code> is the package prefix that was settled on by the group that defined the SAX API.</div></td></tr>  <tr align="left">    <td><a name="wp64234"> </a><div class="pCellBody"><code class="cCode"> org.xml.sax.ext</code></div></td>    <td><a name="wp64236"> </a><div class="pCellBody">Defines SAX extensions that are used when doing more sophisticated SAX processing, for example, to process a document type definitions (DTD) or to see the detailed syntax for a file.</div></td></tr>  <tr align="left">    <td><a name="wp64238"> </a><div class="pCellBody"><code class="cCode"> org.xml.sax.helpers</code></div></td>    <td><a name="wp64240"> </a><div class="pCellBody"> Contains helper classes that make it easier to use SAX--for example, by defining a default handler that has null-methods for all of the interfaces, so you only need to override the ones you actually want to implement.</div></td></tr>  <tr align="left">    <td><a name="wp64242"> </a><div class="pCellBody"><code class="cCode"> javax.xml.parsers</code></div></td>    <td><a name="wp64244"> </a><div class="pCellBody">Defines the <code class="cCode">SAXParserFactory</code> class which returns the <code class="cCode">SAXParser</code>. Also defines exception classes for reporting errors.</div></td></tr></table></div><p class="pBody"></p>    </blockquote>   <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="JAXPIntro3.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JAXPIntro5.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font>  </body></html>

⌨️ 快捷键说明

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