📄 1_read.html
字号:
<blockquote> <p> </p></blockquote>Now, add the code highlighted below to get a instance of a builder, and use it to parse the specified file: <blockquote> <p></p> <pre>try { <b>DocumentBuilder builder = factory.newDocumentBuilder();</b> <b>document = builder.parse( new File(argv[0]) );</b><br>} catch (SAXParseException spe) {</pre></blockquote><h3><b><a name="write"></a></b>Write out the XML</h3><p>At this point, the code has the ability to read in and parse an XML document. To write out the document for inspection, you'll need to step outside the DOM level 1 standard that makes up the DOM section of JAXP. DOM write operations are not specified until DOM level 3. To get by in the meantime, you'll cast the Document object returned by DocumentBuilder to the real object that the reference implementation returns: XmlDocument.</p><blockquote> <p><img src="../images/sun.gif" width="62" height="29"> <b>Note:</b><br> This material is specific to Project X, Sun's reference implementation for the JAXP standard. The material in this section is <i>not</i> part of the standard. Instead, it represents helpful functionality that you may need to take advantage of until some equivalent mechanism is standardized. Because it is not part of the JAXP standard, the functionality described here may very well <i>not</i> exist in other JAXP-standard parsers. In fact, as standards evolve, future versions of the JAXP reference implementation could employ different mechanisms to achieve the same goals.</p></blockquote><h4><b><a name="XmlDocument"></a></b>Use XmlDocument</h4><p>Start by adding the import statement that defines the class:</p><blockquote> <p></p> <pre>import org.w3c.dom.Document;import org.w3c.dom.DOMException;<br><b>import com.sun.xml.tree.XmlDocument;</b><br>public class DomEcho{ </pre></blockquote><p>(The <tt>com.sun.</tt> prefix on that class is your tipoff to the fact that you are moving outside the JAXP standard, and making use of a feature in Sun's reference implementation.) <br></p><p>Next, add the code highlighted below to cast the document object to XmlDocument and write it out:</p><blockquote> <pre>try { DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse( new File(argv[0]) ); <b>XmlDocument xdoc = (XmlDocument) document; xdoc.write (System.out);</b>} catch (SAXParseException spe) {</pre></blockquote><h4><b><a name="run"></a>Run the Program</b></h4><p>Throughout most of the DOM tutorial, you'll be using the sample slideshows you created in the SAX section. In particular, you'll use <a href="samples/slideSample01.xml"><code>slideSample01.xml</code></a>, a simple XML file without nothing much in it, and <a href="samples/slideSample10.xml"><code>slideSample10.xml</code></a>, a more complex example that includes a DTD, processing instructions, entity references, and a CDATA section.</p><p>For instructions on how to compile and run your program, see <a href="../sax/2a_echo.html#compiling">Compiling the Program</a> and <a href="../sax/2a_echo.html#running">Running the Program</a>, from the SAX tutorial. Substitute "DomEcho" for "Echo" as the name of the program, and you're ready to roll. When you run the program on slideSample01.xml, this is the output you see:</p><blockquote> <p></p> <pre><?xml version="1.0" encoding="UTF-8"?><!-- A SAMPLE set of slides --><slideshow title="Sample Slide Show" date="Date of publication" author="Yours Truly"> <!-- TITLE SLIDE --> <slide type="all"> <title>Wake up to WonderWidgets!</title> </slide> <!-- OVERVIEW --> <slide type="all"> <title>Overview</title> <item>Why <em>WonderWidgets</em> are great </item> <item /> <item>Who <em>buys</em> WonderWidgets </item> </slide></slideshow></pre></blockquote><p>When you run the program on slideSample10.xml, the result is pretty similar. In particular, note that the entity reference stayed as it was originally written (it was not replace with the entity text): </p><blockquote> <p></p> <pre><item> <b>&copyright;</b></item></pre> </blockquote>Also, notice that the CDATA section has been preserved: <blockquote> <p></p> <pre> <item> <b><![CDATA[</b>Diagram: frobmorten <------------ fuznaten | <3> ^ | <1> | <1> = fozzle V | <2> = framboze staten--------------------+ <3> = frenzle <2> <b>]]></b> </item></pre></blockquote><h3><a name="addl"></a>Additional Information</h3><p>Now that you have successfully read in a DOM, there are one or two more things you need to know in order to use DocumentBuilder effectively. Namely, you need to know about:</p><ul> <li>Configuring the Factory</li> <li>Handling Validation Errors</li></ul><h4><a name="config"></a>Configuring the Factory</h4><p>By default, the factory returns a nonvalidating parser that knows nothing about namespaces. To get a <a href="../glossary.html#validatingParser">validating parser</a>, and/or one that understands <a href="../glossary.html#namespace">namespace</a>s, you configure the factory to set either or both of those options using the command(s) highlighted below: </p><blockquote> <pre>public static void main (String argv []){ if (argv.length != 1) { ... } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); <b>factory.setValidating(true);</b> <b>factory.setNamespaceAware(true);</b> try {<br> ...</pre> <p><b>Note: </b><br> 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 ParserConfigurationException when you attempt to obtain a parser instance.</p></blockquote><p>You'll be learning more about how to use namespaces in the last section of the DOM tutorial, <a href="6_ns.html">Using Namespaces</a>. To complete this section, though, you'll want to learn something about...</p><h4><a name="val"></a>Handling Validation Errors</h4><p>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><p>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 exactly the same error handling mechanisms as you used for a SAX app. In particular, you need to use the DocumentBuilder's <code>setErrorHandler</code> method to supply it with an object that implements the SAX ErrorHandler interface.</p><blockquote> <p>Note:<br> DocumentBuilder also has a <tt>setEntityResolver</tt> method you can use </p></blockquote><p>The code below uses an anonymous inner class adapter to provide that ErrorHandler. The highlighted code is the part that makes sure validation errors generate an exception.</p><blockquote> <pre>builder.setErrorHandler( new org.xml.sax.ErrorHandler() {<br> // ignore fatal errors (an exception is guaranteed) public void fatalError(SAXParseException exception) throws SAXException { }<new> <b>// treat validation errors as fatal</b></new><new> <b>public void error (SAXParseException e)</b></new><b><new> throws SAXParseException</new><new> {</new> <new>throw e;</new><new></new><new> }</new></b><new></new><new> // dump warnings too</new><new> public void warning (SAXParseException err)</new><new> throws SAXParseException</new><new> {</new> <new>System.out.println ("** Warning"</new> <new>+ ", line " + err.getLineNumber ()</new> <new>+ ", uri " + err.getSystemId ());</new> <new>System.out.println(" " + err.getMessage ());</new><new> }</new> }); </pre></blockquote><p>This code uses an anonymous inner class to generate an instance of an object that implements the ErrorHandler interface. Since it has no class name, it's "anonymous". You can think of it as an "ErrorHandler" instance, although technically it's a no-name instance that implements the specified interface. The code is substantially the same as that described the <a href="../sax/3_error.html">Handling Errors</a> section of the SAX tutorial. For a more background on validation issues, refer to <a href="../sax/6_val.html">Using the Validating Parser</a> in that part of the tutorial.</p><blockquote> <p><b>Note:</b><br> Inner classes are supported in version 1.2 and later versions of the Java Platform. If you are coding for version 1.1, create an external class that implements ErrorHandler as shown above, and use that.</p></blockquote><h3><a name="look"></a>Looking Ahead</h3><p>At this point, you have successfully parsed an XML document and written it out. To do anything useful with the DOM, though, you will to need to know more about it's structure. For example, how do the entity references and CDATA sections appear in the DOM?</p><p>Another interesting question is: How can you convert an existing data structure into an XML document? You'll get answers to those questions in the sections ahead. </p><p>In the next section, you'll take a quick look at a mechanism you can use in the JAXP reference implementation (Project X) to convert an arbitrary data structure into XML (assuming that you already have a program capable of reading that structure). In the section after that, you'll display the DOM in a JTree so you can begin to understand its internal structure.<br></p><blockquote> <p> <hr size=4> <p></p></blockquote><p> <p> <table width="100%"> <tr> <td align=left> <a href="index.html"><img src="../images/PreviousArrow.gif" width=26 height=26 align=top border=0 alt="Previous | "></a><ahref="2_anydata.html"><img src="../images/NextArrow.gif" width=26 height=25 align=top border=0 alt="Next | "></a><a href="../alphaIndex.html"><img src="../images/xml_IDX.gif" width=26 height=26 align=top border=0 alt="Index | "></a><a href="../TOC.html"><imgsrc="../images/xml_TOC.gif" width=26 height=26 align=top border=0 alt="TOC | "></a><a href="../index.html"><imgsrc="../images/xml_Top.gif" width=26 height=26 align=top border=0 alt="Top | "></a> </td> <td align=right><strong><em><a href="index.html">Top</a></em></strong> <a href="../TOC.html#intro"><strong><em>Contents</em></strong></a> <a href="../TOC.html#intro"><strong><em></em></strong></a> <a href="../alphaIndex.html"><strong><em>Index</em></strong></a> <a href="../glossary.html"><strong><em>Glossary</em></strong></a></td> </tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -