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

📄 jaxpsax9.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
To specify the schema definition in the document, you would create XML like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;<code class="cVariable">documentRoot</code>&nbsp;&nbsp;<code class="cCodeBold">xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</code>&nbsp;&nbsp;<code class="cCodeBold">xsi:noNamespaceSchemaLocation</code>=&#39;<code class="cVariable">YourSchemaDefinition</code>.xsd&#39;&gt;&nbsp;&nbsp;...<a name="wp91055"> </a></pre></div><a name="wp91050"> </a><p class="pBody">The first attribute defines the XML NameSpace (xmlns) prefix, &quot;xsi&quot;, where &quot;xsi&quot; stands for &quot;XML Schema Instance&quot;. The second line specifies the schema to use for elements in the document that do <span style="font-style: italic">not</span> have a namespace prefix -- that is, for the elements you typically define in any simple, uncomplicated XML document.</p><hr><a name="wp91091"> </a><p class="pNote">Note: You'll be learning about namespaces in <a  href="JAXPDOM8.html#wp76446">Validating with XML Schema</a>. For now, think of these attributes as the &quot;magic incantation&quot; you use to validate a simple XML file that doesn't use them. Once you've learned more about namespaces, you'll see how to use XML Schema to validate complex documents that use them. Those ideas are discussed in <a  href="JAXPDOM8.html#wp63997">Validating with Multiple Namespaces</a>.</p><hr><a name="wp90899"> </a><p class="pBody">You can also specify the schema file in the application, using code like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">static final String <code class="cCodeBold">JAXP_SCHEMA_SOURCE</code> =&nbsp;&nbsp;&nbsp;&nbsp;&quot;http://java.sun.com/xml/jaxp/properties/schemaSource&quot;;...SAXParser saxParser = spf.newSAXParser();...<code class="cCodeBold">saxParser.setProperty</code>(<code class="cCodeBold">JAXP_SCHEMA_SOURCE</code>,&nbsp;&nbsp;&nbsp;&nbsp;new File(schemaSource));<a name="wp90900"> </a></pre></div><a name="wp90925"> </a><p class="pBody">Now that you know how to make use of an XML Schema definition, we'll turn our attention to the kinds of errors you can see when the application is validating its incoming data. To that, you'll use a Document Type Definition (DTD) as you experiment with validation.</p><a name="wp90292"> </a><h3 class="pHeading2">Experimenting with Validation Errors</h3><a name="wp65321"> </a><p class="pBody">To see what happens when the XML document does not specify a DTD, remove the <code class="cCode">DOCTYPE</code> statement from the XML file and run the Echo program on it.</p><hr><a name="wp65322"> </a><p class="pNote">Note: The output shown here is contained in <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10-01.txt" target="_blank">Echo10-01.txt</a></code>. (The browsable version is <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10-01.html" target="_blank">Echo10-01.html</a></code>.)</p><hr><a name="wp65323"> </a><p class="pBody">The result you see looks like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;** Parsing error, line 9, uri .../slideSample01.xml&nbsp;&nbsp;Document root element &quot;slideshow&quot;, must match DOCTYPE root &quot;null&quot;<a name="wp72070"> </a></pre></div><hr><a name="wp70796"> </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="wp68533"> </a><p class="pBody">This message says that the root element of the document must match the element specified in the <code class="cCode">DOCTYPE</code> declaration. That declaration specifies the document's DTD. Since you don't have one yet, it's value is &quot;null&quot;. In other words, the message is saying that you are trying to validate the document, but no DTD has been declared, because no <code class="cCode">DOCTYPE</code> declaration is present.</p><a name="wp72093"> </a><p class="pBody">So now you know that a DTD is a requirement for a valid document. That makes sense. What happens when you run the parser on your current version of the slide presentation, with the DTD specified?</p><hr><a name="wp65327"> </a><p class="pNote">Note: The output shown here is produced using <code class="cCode"><a  href="../examples/xml/samples/slideSample07.xml" target="_blank">slideSample07.xml</a></code>, as described in <a  href="IntroXML4.html#wp68336">Referencing Binary Entities</a>. The output is contained in <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10-07.txt" target="_blank">Echo10-07.txt</a></code>. (The browsable version is <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10-07.html" target="_blank">Echo10-07.html</a></code>.)</p><hr><a name="wp65328"> </a><p class="pBody">This time, the parser gives a different error message:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">  ** Parsing error, line 29, uri file:...&nbsp;&nbsp;The content of element type &quot;slide&quot; must match &quot;(image?,title,item*)<a name="wp68935"> </a></pre></div><hr><a name="wp70801"> </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="wp68937"> </a><p class="pBody">This message says that the element found at line 29 (<code class="cCode">&lt;item&gt;</code>) does not match the definition of the <code class="cCode">&lt;slide&gt;</code> element in the DTD. The error occurs because the definition says that the <code class="cCode">slide</code> element requires a <code class="cCode">title</code>. That element is not optional, and the copyright slide does not have one. To fix the problem, add the question mark highlighted below to make <code class="cCode">title</code> an optional element:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;!ELEMENT slide (image?, title<code class="cCodeBold">?</code>, item*)&gt;<a name="wp66998"> </a></pre></div><a name="wp66999"> </a><p class="pBody">Now what happens when you run the program?</p><hr><a name="wp65333"> </a><p class="pNote">Note: You could also remove the copyright slide, which produces the same result shown below, as reflected in<code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10-06.txt" target="_blank"> Echo10-06.txt</a></code>. (The browsable version is <code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10-06.html" target="_blank">Echo10-06.html</a></code>.)</p><hr><a name="wp65334"> </a><p class="pBody">The answer is that everything runs fine until the parser runs into the <code class="cCode">&lt;em&gt;</code> tag contained in the overview slide. Since that tag was not defined in the DTD, the attempt to validate the document fails. The output looks like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">  ...&nbsp;&nbsp;ELEMENT: &lt;title&gt;&nbsp;&nbsp;CHARS:   Overview&nbsp;&nbsp;END_ELM: &lt;/title&gt;&nbsp;&nbsp;ELEMENT: &lt;item&gt;&nbsp;&nbsp;CHARS:   Why <code class="cCodeBold">** Parsing error, line 28</code>, uri: ...<code class="cCodeBold">Element &quot;em&quot; must be declared.</code>org.xml.sax.SAXParseException: ......<a name="wp65335"> </a></pre></div><hr><a name="wp68941"> </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="wp65336"> </a><p class="pBody">The error message identifies the part of the DTD that caused validation to fail. In this case it is the line that defines an <code class="cCode">item</code> element as <code class="cCode">(#PCDATA | item)</code>. </p><a name="wp65337"> </a><p class="pDefinition"> <span style="font-weight: bold">Exercise: </span>Make a copy of the file and remove all occurrences of <code class="cCode">&lt;em&gt;</code> from it. Can the file be validated now? (In the next section, you'll learn how to define parameter entries so that we can use XHTML in the elements we are defining as part of the slide presentation.)</p><a name="wp65339"> </a><h3 class="pHeading2">Error Handling in the Validating Parser</h3><a name="wp65340"> </a><p class="pBody">It is important to recognize that the only reason an exception is thrown when the file fails validation is as a result of the error-handling code you entered in the early stages of this tutorial. That code is reproduced below:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public void error(SAXParseException e)throws SAXParseException{&nbsp;&nbsp;<code class="cCodeBold">throw e;</code>}<a name="wp65341"> </a></pre></div><a name="wp65342"> </a><p class="pBody">If that exception is not thrown, the validation errors are simply ignored.</p><a name="wp65343"> </a><p class="pDefinition"><span style="font-weight: bold">Exercise: </span>Try commenting out the line that throws the exception. What happens when you run the parser now?</p><a name="wp65344"> </a><p class="pBody">In general, a SAX parsing <span style="font-style: italic">error</span> is a validation error, although we have seen that it can also be generated if the file specifies a version of XML that the parser is not prepared to handle. The thing to remember is that your application will not generate a validation exception unless you supply an error handler like the one above.</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="JAXPSAX8.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="JAXPSAX10.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 + -