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

📄 jaxpsax9.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<?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 Validating Parser</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="JAXPSAX8.html" />    <link rel="Next" href="JAXPSAX10.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="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">    <blockquote><a name="wp65302"> </a><h2 class="pHeading1">Using the Validating Parser</h2><a name="wp65303"> </a><p class="pBody">By now, you have done a lot of experimenting with the nonvalidating parser. It's time to have a look at the validating parser and find out what happens when you use it to parse the sample presentation. </p><a name="wp65304"> </a><p class="pBody">Two things to understand about the validating parser at the outset are: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp65305"> </a><div class="pSmartList1"><li>A schema or Document Type Definition (DTD) is required.</li></div><a name="wp65306"> </a><div class="pSmartList1"><li>Since the schema/DTD is present, the <code class="cCode">ignorableWhitespace</code> method is invoked whenever possible.</li></div></ul></div><a name="wp65308"> </a><h3 class="pHeading2">Configuring the Factory</h3><a name="wp65309"> </a><p class="pBody">The first step is modify the Echo program so that it uses the validating parser instead of the nonvalidating parser.</p><hr><a name="wp65310"> </a><p class="pNote">Note: The code in this section is contained in<code class="cCode"><a  href="../examples/jaxp/sax/samples/Echo10.java" target="_blank"> Echo10.java</a></code>.</p><hr><a name="wp65311"> </a><p class="pBody">To use the validating parser, make the changes highlighted below:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public static void main(String argv[]){&nbsp;&nbsp;if (argv.length != 1) {&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;}&nbsp;&nbsp;<code class="cCodeStruck">// Use the default (non-validating) parser</code>&nbsp;&nbsp;<code class="cCodeBold">// Use the validating parser</code>&nbsp;&nbsp;SAXParserFactory factory = SAXParserFactory.newInstance();&nbsp;&nbsp;<code class="cCodeBold">factory.setValidating(true);</code>&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;...<a name="wp65312"> </a></pre></div><a name="wp65313"> </a><p class="pBody">Here, you configured the factory so that it will produce a validating parser when <code class="cCode">newSAXParser</code> is invoked. You can also configure it to return a namespace-aware parser using <code class="cCode">setNamespaceAware(true)</code>. Sun's implementation supports any combination of configuration options. (If a combination is not supported by any particular implementation, it is required to generate a factory configuration error.)</p><a name="wp90294"> </a><h3 class="pHeading2">Validating with XML Schema</h3><a name="wp92449"> </a><p class="pBody">Although a full treatment of XML Schema is beyond the scope of this tutorial, this section will show you the steps you need to take to validate an XML document using an existing schema written in the XML Schema language. (To learn more about XML Schema, you can review the online tutorial, <span style="font-style: italic">XML Schema Part 0: Primer</span><a  href="http://www.w3.org/TR/xmlschema-0/" target="_blank">, at http://www.w3.org/TR/xmlschema-0/. You can also </a>examine the sample programs that are part of the JAXP download. They use a simple XML Schema definition to validate personnel data stored in an XML file.)</p><hr><a name="wp92505"> </a><p class="pNote">Note: There are multiple schema-definition languages, including RELAX NG, Schematron, and the W3C &quot;XML Schema&quot; standard. (Even a DTD qualifies as a &quot;schema&quot;, although it is the only one that does not use XML syntax to describe schema constraints.) However, &quot;XML Schema&quot; presents us with a terminology challenge. While the phrase &quot;XML Schema schema&quot; would be precise, we'll use the phrase &quot;XML Schema definition&quot; to avoid the appearance of redundancy. </p><hr><a name="wp90707"> </a><p class="pBody">To be notified of validation errors in an XML document, the parser factory must be configured to create a validating parser, as shown in the previous section. In addition,</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp90849"> </a><div class="pSmartList1"><li>The appropriate properties must be set on the SAX parser.</li></div><a name="wp90850"> </a><div class="pSmartList1"><li>The appropriate error handler must be set.</li></div><a name="wp90856"> </a><div class="pSmartList1"><li>The document must be associated with a schema.</li></div></ol></div><a name="wp90829"> </a><h4 class="pHeading3">Setting the SAX Parser Properties</h4><a name="wp90830"> </a><p class="pBody">It's helpful to start by defining the constants you'll use when setting the properties:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">static final String <code class="cCodeBold">JAXP_SCHEMA_LANGUAGE</code> =&nbsp;&nbsp;&nbsp;&nbsp;&quot;http://java.sun.com/xml/jaxp/properties/schemaLanguage&quot;;static final String <code class="cCodeBold">W3C_XML_SCHEMA</code> =&nbsp;&nbsp;&nbsp;&nbsp;&quot;http://www.w3.org/2001/XMLSchema&quot;;<a name="wp90574"> </a></pre></div><a name="wp90760"> </a><p class="pBody">Next, you need to configure the parser factory to generate a parser that is namespace-aware parser, as well as validating:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...&nbsp;&nbsp;SAXParserFactory factory = SAXParserFactory.newInstance();&nbsp;&nbsp;<code class="cCodeBold">factory.setNamespaceAware(true);</code>&nbsp;&nbsp;factory.setValidating(true);<a name="wp90761"> </a></pre></div><a name="wp90765"> </a><p class="pBody">You'll learn more about namespaces in <a  href="JAXPDOM8.html#wp76446">Validating with XML Schema</a>. For now, understand that schema validation is a namespace-oriented process. Since JAXP-compliant parsers are not namespace-aware by default, it is necessary to set the property for schema validation to work.</p><a name="wp90816"> </a><p class="pBody">The last step is to configure the parser to tell it which schema language to use. Here, you will use the constants you defined earlier to specify the W3C's XML Schema language:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">saxParser.setProperty(<code class="cCodeBold">JAXP_SCHEMA_LANGUAGE</code>, <code class="cCodeBold">W3C_XML_SCHEMA</code>);<a name="wp90878"> </a></pre></div><a name="wp90912"> </a><p class="pBody">In the process, however, there is an extra error to handle. You'll take a look at that error next.</p><a name="wp91010"> </a><h4 class="pHeading3">Setting up the Appropriate Error Handling</h4><a name="wp91011"> </a><p class="pBody">In addition to the error handling you've already learned about, there is one error that can occur when you are configuring the parser for schema-based validation. If the parser is not 1.2 compliant, and therefore does not support XML Schema, it could throw a <code class="cCode">SAXNotRecognizedException</code>. </p><a name="wp90967"> </a><p class="pBody">To handle that case, you wrap the <code class="cCode">setProperty()</code> statement in a <code class="cCode">try</code>/<code class="cCode">catch</code> block, as shown in the code highlighted below.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...SAXParser saxParser = factory.newSAXParser();<code class="cCodeBold">try {</code>&nbsp;&nbsp;saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);} <code class="cCodeBold">catch (SAXNotRecognizedException x) {</code>&nbsp;&nbsp;// Happens if the parser does not support JAXP 1.2&nbsp;&nbsp;...<code class="cCodeBold">}</code>...<a name="wp90980"> </a></pre></div><a name="wp90895"> </a><h4 class="pHeading3">Associating a Document with A Schema</h4><a name="wp90896"> </a><p class="pBody">Now that the program is ready to validate the data using an XML Schema definition, it is only necessary to ensure that the XML document is associated with one. There are two ways to do that:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp90987"> </a><div class="pSmartList1"><li>With a schema declaration in the XML document.</li></div><a name="wp90993"> </a><div class="pSmartList1"><li>By specifying the schema to use in the application.</li></div></ul></div><hr><a name="wp90897"> </a><p class="pNote">Note: When the application specifies the schema to use, it overrides any schema declaration in the document.</p><hr><a name="wp90997"> </a><p class="pBody">

⌨️ 快捷键说明

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