📄 jaxpsax12.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>Using the DTDHandler and EntityResolver</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="JAXPSAX11.html" /> <link rel="Next" href="JAXPSAX13.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="JAXPSAX11.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="JAXPSAX13.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="wp65656"> </a><h2 class="pHeading1">Using the DTDHandler and EntityResolver</h2><a name="wp65657"> </a><p class="pBody">In this section of the tutorial, we'll carry on a short discussion of the two remaining SAX event handlers: <code class="cCode">DTDHandler</code> and <code class="cCode">EntityResolver</code>. The <code class="cCode">DTDHandler</code> is invoked when the DTD encounters an unparsed entity or a notation declaration. The <code class="cCode">EntityResolver</code> comes into play when a URN (public ID) must be resolved to a URL (system ID).</p><a name="wp65659"> </a><h3 class="pHeading2">The DTDHandler API</h3><a name="wp65663"> </a><p class="pBody">In the section <a href="JAXPSAX8.html#wp90267">Choosing your Parser Implementation</a> you saw a method for referencing a file that contains binary data, like an image file, using MIME data types. That is the simplest, most extensible mechanism to use. For compatibility with older SGML-style data, though, it is also possible to define an unparsed entity. </p><a name="wp65664"> </a><p class="pBody">The <code class="cCode">NDATA</code> keyword defines an unparsed entity, like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> <!ENTITY myEntity SYSTEM "..URL.." <code class="cCodeBold">NDATA gif</code>><a name="wp65665"> </a></pre></div><a name="wp65666"> </a><p class="pBody">The <code class="cCode">NDATA</code> keyword says that the data in this entity is not parsable XML data, but is instead data that uses some other notation. In this case, the notation is named "gif". The DTD must then include a declaration for that notation, which would look something like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> <!<code class="cCodeBold">NOTATION gif</code> SYSTEM "..URL.."><a name="wp65667"> </a></pre></div><a name="wp65668"> </a><p class="pBody">When the parser sees an unparsed entity or a notation declaration, it does nothing with the information except to pass it along to the application using the <code class="cCode">DTDHandler</code> interface. That interface defines two methods:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold">notationDecl</code>(String name, String publicId, String systemId) <a name="wp65669"> </a><code class="cCodeBold">unparsedEntityDecl</code>(String name, String publicId, String systemId, String notationName) <a name="wp65670"> </a></pre></div><a name="wp65671"> </a><p class="pBody">The <code class="cCode">notationDecl</code> method is passed the name of the notation and either the public or system identifier, or both, depending on which is declared in the DTD. The <code class="cCode">unparsedEntityDecl</code> method is passed the name of the entity, the appropriate identifiers, and the name of the notation it uses. </p><hr><a name="wp65672"> </a><p class="pNote">Note: The DTDHandler interface is implemented by the <code class="cCode">DefaultHandler</code> class.</p><hr><a name="wp65673"> </a><p class="pBody">Notations can also be used in attribute declarations. For example, the following declaration requires notations for the GIF and PNG image-file formats:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><!ENTITY image EMPTY><!ATTLIST image ... type<code class="cCodeBold"> NOTATION (gif | png) "gif"</code>><a name="wp65674"> </a></pre></div><a name="wp65675"> </a><p class="pBody">Here, the <code class="cCode">type</code> is declared as being either <code class="cCode">gif</code>, or <code class="cCode">png</code>. The default, if neither is specified, is <code class="cCode">gif</code>. </p><a name="wp65676"> </a><p class="pBody">Whether the notation reference is used to describe an unparsed entity or an attribute, it is up to the application to do the appropriate processing. The parser knows nothing at all about the semantics of the notations. It only passes on the declarations.</p><a name="wp65678"> </a><h3 class="pHeading2">The EntityResolver API</h3><a name="wp65679"> </a><p class="pBody">The <code class="cCode">EntityResolver</code> API lets you convert a public ID (URN) into a system ID (URL). Your application may need to do that, for example, to convert something like <code class="cCode">href="urn:/someName"</code> into <code class="cCode">"http://someURL"</code>. </p><a name="wp65680"> </a><p class="pBody">The <code class="cCode">EntityResolver</code> interface defines a single method:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"> <code class="cCodeBold">resolveEntity</code>(String publicId, String systemId)<a name="wp65681"> </a></pre></div><a name="wp65682"> </a><p class="pBody">This method returns an <code class="cCode">InputSource</code> object, which can be used to access the entity's contents. Converting an URL into an <code class="cCode">InputSource</code> is easy enough. But the URL that is passed as the system ID will be the location of the original document which is, as likely as not, somewhere out on the Web. To access a local copy, if there is one, you must maintain a catalog somewhere on the system that maps names (public IDs) into local URLs.</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="JAXPSAX11.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="JAXPSAX13.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 + -