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

📄 jaxpdom8.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>Validating with XML Schema</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="JAXPDOM7.html" />    <link rel="Next" href="JAXPDOM9.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="JAXPDOM7.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="JAXPDOM9.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="wp76446"> </a><h2 class="pHeading1">Validating with XML Schema</h2><a name="wp76447"> </a><p class="pBody">You're now ready to take a deeper look at the process of XML Schema validation. 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 XML Schema definition. (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="wp78783"> </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 semblance of redundancy. </p><hr><a name="wp76667"> </a><p class="pBody">At the end of this section, you'll also learn how to use an XML Schema definition to validate a document that contains elements from multiple namespaces.</p><a name="wp76448"> </a><h3 class="pHeading2">Overview of the Validation Process</h3><a name="wp76679"> </a><p class="pBody">To be notified of validation errors in an XML document,</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp76449"> </a><div class="pSmartList1"><li>The factory must configured, and the appropriate error handler set.</li></div><a name="wp76451"> </a><div class="pSmartList1"><li>The document must be associated with at least one schema, and possibly more.</li></div></ol></div><a name="wp76452"> </a><h3 class="pHeading2">Configuring the DocumentBuilder Factory </h3><a name="wp76633"> </a><p class="pBody">It's helpful to start by defining the constants you'll use when configuring the factory. (These are same constants you define when using XML Schema for SAX parsing.)</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="wp76625"> </a></pre></div><a name="wp76661"> </a><p class="pBody">Next, you need to configure <code class="cCode">DocumentBuilderFactory</code> to generate a namespace-aware, validating parser that uses XML Schema:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">...&nbsp;&nbsp;DocumentBuilderFactory factory =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DocumentBuilderFactory.newInstance()&nbsp;&nbsp;<code class="cCodeBold">factory.setNamespaceAware(true);&nbsp;&nbsp;factory.setValidating(true);try {&nbsp;&nbsp;factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);} catch (IllegalArgumentException x) {&nbsp;&nbsp;// Happens if the parser does not support JAXP 1.2&nbsp;&nbsp;...}</code><a name="wp76710"> </a></pre></div><a name="wp76460"> </a><p class="pBody">Since JAXP-compliant parsers are not namespace-aware by default, it is necessary to set the property for schema validation to work. You also set a factory attribute specify the parser language to use. (For SAX parsing, on the other hand, you set a property on the parser generated by the factory.)</p><a name="wp76468"> </a><h4 class="pHeading3">Associating a Document with a Schema</h4><a name="wp76469"> </a><p class="pBody">Now that the program is ready to validate with an XML Schema definition, it is only necessary to ensure that the XML document is associated with (at least) one. There are two ways to do that:</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp76470"> </a><div class="pSmartList1"><li>With a schema declaration in the XML document.</li></div><a name="wp76471"> </a><div class="pSmartList1"><li>By specifying the schema(s) to use in the application.</li></div></ol></div><hr><a name="wp76472"> </a><p class="pNote">Note: When the application specifies the schema(s) to use, it overrides any schema declarations in the document.</p><hr><a name="wp76473"> </a><p class="pBody">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><code class="cCodeBold">&nbsp;&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&nbsp;&nbsp;xsi:noNamespaceSchemaLocation</code>=&#39;<code class="cVariable">YourSchemaDefinition</code>.xsd&#39;&gt;&nbsp;&nbsp;...<a name="wp76474"> </a></pre></div><a name="wp76475"> </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. (You'll see how to deal with multiple namespaces in the next section.)</p><a name="wp76480"> </a><p class="pBody">You can also specify the schema file in the application, like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">static final String schemaSource = &quot;<code class="cVariable">YourSchemaDefinition</code>.xsd&quot;;static final String <code class="cCodeBold">JAXP_SCHEMA_SOURCE</code> =&nbsp;&nbsp;&nbsp;&nbsp;&quot;http://java.sun.com/xml/jaxp/properties/schemaSource&quot;;...DocumentBuilderFactory factory =&nbsp;&nbsp;&nbsp;&nbsp;DocumentBuilderFactory.newInstance()...<code class="cCodeBold">factory.setAttribute</code>(<code class="cCodeBold">JAXP_SCHEMA_SOURCE</code>,&nbsp;&nbsp;&nbsp;&nbsp;new File(schemaSource));<a name="wp76481"> </a></pre></div><a name="wp76482"> </a><p class="pBody">Here, too, there are mechanisms at your disposal that will let you specify multiple schemas. We'll take a look at those next.</p><a name="wp63997"> </a><h3 class="pHeading2">Validating with Multiple Namespaces</h3><a name="wp76526"> </a><p class="pBody">Namespaces let you combine elements that serve different purposes in the same document, without having to worry about overlapping names. </p><hr><a name="wp76943"> </a><p class="pNote">Note: The material discussed in this section also applies to validating when using the SAX parser. You're seeing it here, because at this point you've learned enough about namespaces for the discussion to make sense.</p>

⌨️ 快捷键说明

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