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

📄 jaxpsax10.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>Parsing a Parameterized DTD</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="JAXPSAX9.html" />    <link rel="Next" href="JAXPSAX11.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="JAXPSAX9.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="JAXPSAX11.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="wp65449"> </a><h2 class="pHeading1">Parsing a Parameterized DTD</h2><a name="wp65450"> </a><p class="pBody">This section uses the Echo program to see what happens when you reference <code class="cCode">xhtml.dtd</code> in <code class="cCode">slideshow2.dtd</code>. It also covers the kinds of warnings that are generated by the SAX parser when a DTD is present.</p><hr><a name="wp95208"> </a><p class="pNote">Note: The XML file used here is <code class="cCode"><a  href="../examples/xml/samples/slideSample08.xml" target="_blank">slideSample08.xml</a></code>, which references <code class="cCode"><a  href="../examples/xml/samples/slideshow2.dtd" target="_blank">slideshow2.dtd</a></code>. The output is contained in <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10-08.txt" target="_blank">Echo10-08.txt</a></code>. (The browsable versions are <code class="cCode"><a  href="../examples/xml/samples/slideSample08-xml.html" target="_blank">slideSample08-xml.html</a></code>, <code class="cCode"><a  href="../examples/xml/samples/slideshow2-dtd.html" target="_blank">slideshow2-dtd.html</a></code>, and <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10-08.html" target="_blank">Echo10-08.html</a></code>.) </p><hr><a name="wp65452"> </a><p class="pBody">When you try to echo the slide presentation, you find that it now contains a new error. The relevant part of the output is shown here (formatted for readability):</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;** Parsing error, line 22, uri: .../slideshow.dtdElement type &quot;title&quot; must not be declared more than once.<a name="wp68986"> </a></pre></div><hr><a name="wp70806"> </a><p class="pNote">Note: The message above was generated by the JAXP 1.2 libraries. If you are using a different parser, the error message is likely to be somewhat different.</p><hr><a name="wp68987"> </a><p class="pBody">The problem is that <code class="cCode">xhtml.dtd</code> defines a <code class="cCode">title</code> element which is entirely different from the <code class="cCode">title</code> element defined in the slideshow DTD. Because there is no hierarchy in the DTD, these two definitions conflict.</p><a name="wp65469"> </a><p class="pBody">The <code class="cCode">slideSample09.xml</code> version solves the problem by changing the name of the slide title. Run the Echo program on that version of the slide presentation. It should run to completion and display output like that shown in Echo10-09.</p><a name="wp65470"> </a><p class="pBody">Congratulations! You have now read a fully validated XML document. The change in that version of the file had the effect of putting the DTD's <code class="cCode">title</code> element into a slideshow &quot;namespace&quot; that you artificially constructed by hyphenating the name, so the <code class="cCode">title</code> element in the &quot;slideshow namespace&quot; (<code class="cCode">slide-title</code>, really) was no longer in conflict with the <code class="cCode">title</code> element in <code class="cCode">xhtml.dtd</code>. </p><hr><a name="wp95304"> </a><p class="pNote">Note: As mentioned in <a  href="IntroXML4.html#wp68796">Using Namespaces</a>, namespaces let you accomplish the same goal without having to rename any elements.</p><hr><a name="wp95313"> </a><p class="pBody">To finish off this section, we'll take a look at the kinds of warnings that the validating parser can produce when processing the DTD. </p><a name="wp65472"> </a><h3 class="pHeading2">DTD Warnings</h3><a name="wp65473"> </a><p class="pBody">As mentioned earlier in this tutorial, warnings are generated only when the SAX parser is processing a DTD. Some warnings are generated only by the validating parser. The nonvalidating parser's main goal is operate as rapidly as possible, but it too generates some warnings. (The explanations that follow tell which does what.)</p><a name="wp65474"> </a><p class="pBody">The XML specification suggests that warnings should be generated as result of:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp65475"> </a><div class="pSmartList1"><li>Providing additional declarations for entities, attributes, or notations.</li></div><a name="wp92398"> </a><p class="pBodyRelative">(Such declarations are ignored. Only the first is used. Also, note that duplicate definitions of <span style="font-style: italic">elements</span> always produce a fatal error when validating, as you saw earlier.) </p><a name="wp65476"> </a><div class="pSmartList1"><li>Referencing an undeclared element type.</li></div><a name="wp65477"> </a><p class="pBodyRelative">(A validity error occurs only if the undeclared type is actually used in the XML document. A warning results when the undeclared element is referenced in the DTD.) </p><a name="wp65478"> </a><div class="pSmartList1"><li>Declaring attributes for undeclared element types.</li></div></ul></div><a name="wp65479"> </a><p class="pBody">The Java XML SAX parser also emits warnings in other cases, such as: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp65480"> </a><div class="pSmartList1"><li>No &lt;!DOCTYPE ...&gt; when validating.</li></div><a name="wp65481"> </a><div class="pSmartList1"><li>Referencing an undefined parameter entity when not validating.</li></div><a name="wp65482"> </a><p class="pBodyRelative">(When validating, an error results. Although nonvalidating parsers are not required to read parameter entities, the Java XML parser does so. Since it is not a requirement, the Java XML parser generates a warning, rather than an error.)</p><a name="wp65483"> </a><div class="pSmartList1"><li>Certain cases where the character-encoding declaration does not look right.</li></div></ul></div><a name="wp65484"> </a><p class="pBody">At this point, you have digested many XML concepts, including DTDs, external entities. You have also learned your way around the SAX parser. The remainder of the SAX tutorial covers advanced topics that you will only need to understand if you are writing SAX-based applications. If your primary goal is to write DOM-based applications, you can skip ahead to <a  href="JAXPDOM.html#wp79994">Document Object Model</a>.</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="JAXPSAX9.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="JAXPSAX11.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 + -