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

📄 jaxpdom3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
&nbsp;&nbsp; } catch (IOException ioe) {&nbsp;&nbsp;&nbsp;&nbsp;// I/O error&nbsp;&nbsp;&nbsp;&nbsp;ioe.printStackTrace();&nbsp;&nbsp;}}// main<a name="wp67019"> </a></pre></div><a name="wp64084"> </a><h4 class="pHeading3">Instantiate the Factory </h4><a name="wp64085"> </a><p class="pBody">Next, add the code highlighted below to obtain an instance of a factory that can give us a document builder:</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;}<code class="cCodeBold">&nbsp;&nbsp;DocumentBuilderFactory factory =&nbsp;&nbsp;&nbsp;&nbsp;DocumentBuilderFactory.newInstance();</code>&nbsp;&nbsp;try {<a name="wp64086"> </a></pre></div><a name="wp64088"> </a><h4 class="pHeading3">Get a Parser and Parse the File</h4><a name="wp64089"> </a><p class="pBody">Now, add the code highlighted below to get a instance of a builder, and use it to parse the specified file:    </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">try {<code class="cCodeBold">&nbsp;&nbsp;DocumentBuilder builder = factory.newDocumentBuilder();&nbsp;&nbsp;document = builder.parse( new File(argv[0]) );</code>} catch (SAXParseException spe) {<a name="wp64090"> </a></pre></div><a name="wp64091"> </a><p class="pDefinitionTerm">Save This File!</p><a name="wp64092"> </a><p class="pDefinition">By now, you should be getting the idea that every JAXP application starts pretty much the same way. You're right! Save this version of the file as a template. You'll use it later on as the basis for an XSLT transformation application.</p><a name="wp64094"> </a><h4 class="pHeading3">Run the Program</h4><a name="wp64095"> </a><p class="pBody">Throughout most of the DOM tutorial, you'll be using the sample slideshows you saw in the SAX section. In particular, you'll use <code class="cCode">slideSample01.xml</code>, a simple XML file with nothing much in it, and <code class="cCode">slideSample10.xml</code>, a more complex example that includes a DTD, processing instructions, entity references, and a CDATA section.</p><a name="wp64098"> </a><p class="pBody">For instructions on how to compile and run your program, see <a  href="JAXPSAX3.html#wp64274">Compiling and Running the Program</a> from the SAX tutorial. Substitute &quot;DomEcho&quot; for &quot;Echo&quot; as the name of the program, and you're ready to roll. </p><a name="wp64103"> </a><p class="pBody">For now, just run the program on <code class="cCode">slideSample01.xml</code>. If it ran without error, you have successfully parsed an XML document and constructed a DOM. Congratulations! </p><hr><a name="wp64104"> </a><p class="pNote">Note: You'll have to take my word for it, for the moment, because at this point you don't have any way to display the results. But that feature is coming shortly... </p><hr><a name="wp64106"> </a><h3 class="pHeading2">Additional Information</h3><a name="wp64107"> </a><p class="pBody">Now that you have successfully read in a DOM, there are one or two more things you need to know in order to use <code class="cCode">DocumentBuilder</code> effectively. Namely, you need to know about:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64108"> </a><div class="pSmartList1"><li>Configuring the Factory</li></div><a name="wp64109"> </a><div class="pSmartList1"><li>Handling Validation Errors</li></div></ul></div><a name="wp64111"> </a><h4 class="pHeading3">Configuring the Factory</h4><a name="wp64112"> </a><p class="pBody">By default, the factory returns a nonvalidating parser that knows nothing about namespaces. To get a validating parser, and/or one that understands namespaces, you configure the factory to set either or both of those options using the command(s) 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;DocumentBuilderFactory factory =&nbsp;&nbsp;&nbsp;&nbsp;DocumentBuilderFactory.newInstance();<code class="cCodeBold">&nbsp;&nbsp;factory.setValidating(true);&nbsp;&nbsp;factory.setNamespaceAware(true);</code>&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;...<a name="wp64113"> </a></pre></div><hr><a name="wp64114"> </a><p class="pNote">Note: JAXP-conformant parsers are not required to support all combinations of those options, even though the reference parser does. If you specify an invalid combination of options, the factory generates a <code class="cCode">ParserConfigurationException</code> when you attempt to obtain a parser instance.</p><hr><a name="wp64115"> </a><p class="pBody">You'll be learning more about how to use namespaces in the last section of the DOM tutorial, <a  href="JAXPDOM8.html#wp76446">Validating with XML Schema</a>. To complete this section, though, you'll want to learn something about...</p><a name="wp64120"> </a><h4 class="pHeading3">Handling Validation Errors</h4><a name="wp64121"> </a><p class="pBody">Remember when you were wading through the SAX tutorial, and all you really wanted to do was construct a DOM? Well, here's when that information begins to pay off.</p><a name="wp64122"> </a><p class="pBody">Recall that the default response to a validation error, as dictated by the SAX standard, is to do nothing. The JAXP standard requires throwing SAX exceptions, so you use exactly the same error handling mechanisms as you used for a SAX application. In particular, you need to use the <code class="cCode">DocumentBuilder</code>'s <code class="cCode">setErrorHandler</code> method to supply it with an object that implements the SAX <code class="cCode">ErrorHandler</code> interface.</p><hr><a name="wp64123"> </a><p class="pNote">Note: <code class="cCode">DocumentBuilder</code> also has a <code class="cCode">setEntityResolver</code> method you can use </p><hr><a name="wp64124"> </a><p class="pBody">The code below uses an anonymous inner class to define that <code class="cCode">ErrorHandler</code>. The highlighted code is the part that makes sure validation errors generate an exception.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">builder.setErrorHandler(&nbsp;&nbsp;new org.xml.sax.ErrorHandler() {&nbsp;&nbsp;&nbsp;&nbsp;// ignore fatal errors (an exception is guaranteed)&nbsp;&nbsp;&nbsp;&nbsp;public void fatalError(SAXParseException exception)&nbsp;&nbsp;&nbsp;&nbsp;throws SAXException {&nbsp;&nbsp;&nbsp;&nbsp;}<code class="cCodeBold">&nbsp;&nbsp;&nbsp;&nbsp;// treat validation errors as fatal&nbsp;&nbsp;&nbsp;&nbsp;public void error(SAXParseException e)&nbsp;&nbsp;&nbsp;&nbsp;throws SAXParseException&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw e;&nbsp;&nbsp;&nbsp;&nbsp;}</code>&nbsp;&nbsp;&nbsp;&nbsp; // dump warnings too&nbsp;&nbsp;&nbsp;&nbsp;public void warning(SAXParseException err)&nbsp;&nbsp;&nbsp;&nbsp;throws SAXParseException&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;** Warning&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot;, line &quot; + err.getLineNumber()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot;, uri &quot; + err.getSystemId());&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;   &quot; + err.getMessage());&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;); <a name="wp64125"> </a></pre></div><a name="wp64126"> </a><p class="pBody">This code uses an anonymous inner class to generate an instance of an object that implements the <code class="cCode">ErrorHandler</code> interface. Since it has no class name, it's &quot;anonymous&quot;. You can think of it as an &quot;ErrorHandler&quot; instance, although technically it's a no-name instance that implements the specified interface. The code is substantially the same as that described in <a  href="JAXPSAX5.html#wp64579">Handling Errors with the Nonvalidating Parser</a>. For a more complete background on validation issues, refer to <a  href="JAXPSAX9.html#wp65302">Using the Validating Parser</a>.</p><a name="wp64134"> </a><h3 class="pHeading2">Looking Ahead</h3><a name="wp64135"> </a><p class="pBody">In the next section, you'll display the DOM structure in a JTree and begin to explore its structure. For example, you'll see how entity references and CDATA sections appear in the DOM. And perhaps most importantly, you'll see how text nodes (which contain the actual data) reside <span style="font-style: italic">under</span> element nodes in a DOM. </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="JAXPDOM2.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="JAXPDOM4.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 + -