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

📄 jaxpxslt5.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
{ return null; }</code><a name="wp64789"> </a></pre></div><a name="wp64790"> </a><p class="pBody">Finally, add the code highlighted below to generate null methods for the remainder of the<code class="cCode"> XmlReader</code> interface. (Most of them are of value to a real SAX parser, but have little bearing on a data-conversion application like this one.) </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">/** Parse an XML document from a system identifier (URI). */<code class="cCodeBold">public void parse(String systemId)throws IOException, SAXException { }</code><a name="wp64791"> </a> /** Return the current DTD handler. */<code class="cCodeBold">public DTDHandler getDTDHandler(){ return null; }</code><a name="wp64792"> </a>/** Return the current entity resolver. */<code class="cCodeBold">public EntityResolver getEntityResolver(){ return null; }</code><a name="wp64793"> </a>/** Allow an application to register an entity resolver. */<code class="cCodeBold">public void setEntityResolver(EntityResolver resolver){ }</code><a name="wp64794"> </a>/** Allow an application to register a DTD event handler. */<code class="cCodeBold">public void setDTDHandler(DTDHandler handler){ }</code><a name="wp64795"> </a>/** Look up the value of a property. */<code class="cCodeBold">public Object getProperty(String name){ return null; }</code><a name="wp64796"> </a>/** Set the value of a property. */<code class="cCodeBold">public void setProperty(String name, Object value){ }</code> <a name="wp64797"> </a>/** Set the state of a feature. */<code class="cCodeBold">public void setFeature(String name, boolean value){ }</code><a name="wp64798"> </a>/** Look up the value of a feature. */<code class="cCodeBold">public boolean getFeature(String name){ return false; }</code> <a name="wp64799"> </a></pre></div><a name="wp64800"> </a><p class="pBody">Congratulations! You now have a parser you can use to generate SAX events. In the next section, you'll use it to construct a SAX source object that will let you transform the data into XML.</p><a name="wp64802"> </a><h3 class="pHeading2">Using the Parser as a SAXSource</h3><a name="wp64803"> </a><p class="pBody">Given a SAX parser to use as an event source, you can (easily!) construct a transformer to produce a result. In this section, you'll modify the <code class="cCode">TransformerApp</code> you've been working with to produce a stream output result, although you could just as easily produce a DOM result.</p><hr><a name="wp64804"> </a><p class="pNote">Note: The code discussed in this section is in <code class="cCode"><a  href="../examples/jaxp/xslt/samples/TransformationApp04.java" target="_blank">TransformationApp04.java</a></code>. The results of running it are in <code class="cCode"><a  href="../examples/jaxp/xslt/samples/TransformationLog04.txt" target="_blank">TransformationLog04.txt</a></code>.</p><hr><a name="wp64805"> </a><p class="pBody">Important!</p><a name="wp68502"> </a><p class="pBody">Make sure you put the <code class="cCode">AddressBookReader</code> aside and open up the <code class="cCode">TransformationApp</code>. The work you do in this section affects the <code class="cCode">TransformationApp</code>! (The look pretty similar, so it's easy to start working on the wrong one.)</p><a name="wp64807"> </a><p class="pBody">Start by making the changes shown below to import the classes you'll need to construct a <code class="cCode">SAXSource</code> object. (You won't be needing the DOM classes at this point, so they are discarded here, although leaving them in doesn't do any harm.)</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; <code class="cCodeBold">import org.xml.sax.ContentHandler;import org.xml.sax.InputSource;</code><code class="cCodeStruck">import org.w3c.dom.Document;import org.w3c.dom.DOMException;</code>...<code class="cCodeStruck">import javax.xml.transform.dom.DOMSource; </code><code class="cCodeBold">import javax.xml.transform.sax.SAXSource; </code>import javax.xml.transform.stream.StreamResult; <a name="wp64808"> </a></pre></div><a name="wp64809"> </a><p class="pBody">Next, remove a few other holdovers from our DOM-processing days, and add the code to create an instance of the <code class="cCode">AddressBookReader</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public class TransformationApp {<code class="cCodeStruck">&nbsp;&nbsp;// Global value so it can be ref&#39;d by the tree-adapter&nbsp;&nbsp;static Document document; </code><a name="wp64810"> </a>&nbsp;&nbsp; public static void main(String argv[])&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp;<code class="cCodeStruck">DocumentBuilderFactory factory =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DocumentBuilderFactory.newInstance();</code>&nbsp;&nbsp;&nbsp;&nbsp;<code class="cCodeStruck">//factory.setNamespaceAware(true);&nbsp;&nbsp;&nbsp;&nbsp;//factory.setValidating(true); </code>&nbsp;&nbsp;&nbsp;&nbsp;// Create the sax &quot;parser&quot;.<code class="cCodeBold">&nbsp;&nbsp;&nbsp;&nbsp;AddressBookReader saxReader = new AddressBookReader();</code>&nbsp;&nbsp;&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File f = new File(argv[0]);&nbsp;&nbsp;&nbsp;&nbsp;<code class="cCodeStruck">&nbsp;&nbsp;DocumentBuilder builder =&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;factory.newDocumentBuilder();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;document = builder.parse(f);</code><a name="wp64811"> </a></pre></div><a name="wp64812"> </a><p class="pBody">Guess what! You're almost done. Just a couple of steps to go. Add the code highlighted below to construct a <code class="cCode">SAXSource</code> object: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">// Use a Transformer for output...Transformer transformer = tFactory.newTransformer();<code class="cCodeBold">// Use the parser as a SAX source for inputFileReader fr = new FileReader(f);BufferedReader br = new BufferedReader(fr);InputSource inputSource = new InputSource(br);SAXSource source = new SAXSource(saxReader, inputSource);</code>StreamResult result = new StreamResult(System.out);transformer.transform(source, result);<a name="wp64813"> </a></pre></div><a name="wp64814"> </a><p class="pBody">Here, you constructed a buffered reader (as mentioned earlier) and encapsulated it in an input source object. You then created a <code class="cCode">SAXSource</code> object, passing it the reader and the <code class="cCode">InputSource</code> object, and passed that to the transformer.</p><a name="wp64815"> </a><p class="pBody">When the application runs, the transformer will configure itself as the <code class="cCode">ContentHandler</code> for the SAX parser (the <code class="cCode">AddressBookReader</code>) and tell the parser to operate on the <code class="cCode">inputSource</code> object. Events generated by the parser will then go to the transformer, which will do the appropriate thing and pass the data on to the result object. </p><a name="wp64816"> </a><p class="pBody">Finally, remove the exceptions you no longer need to worry about, since the <code class="cCode">TransformationApp</code> no longer generates them:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeStruck">catch (SAXParseException spe) {&nbsp;&nbsp;// Error generated by the parser&nbsp;&nbsp;System.out.println(&quot;\n** Parsing error&quot;&nbsp;&nbsp;&nbsp;&nbsp;+ &quot;, line &quot; + spe.getLineNumber()&nbsp;&nbsp;&nbsp;&nbsp;+ &quot;, uri &quot; + spe.getSystemId());&nbsp;&nbsp;System.out.println(&quot;   &quot; + spe.getMessage() );&nbsp;&nbsp;&nbsp;&nbsp;// Use the contained exception, if any&nbsp;&nbsp;Exception  x = spe;&nbsp;&nbsp;if (spe.getException() != null)&nbsp;&nbsp;&nbsp;&nbsp;x = spe.getException();&nbsp;&nbsp;x.printStackTrace();} catch (SAXException sxe) {&nbsp;&nbsp;// Error generated by this application&nbsp;&nbsp;// (or a parser-initialization error)&nbsp;&nbsp;Exception  x = sxe;&nbsp;&nbsp;if (sxe.getException() != null)&nbsp;&nbsp;&nbsp;&nbsp;x = sxe.getException();&nbsp;&nbsp;x.printStackTrace();} catch (ParserConfigurationException pce) {&nbsp;&nbsp;// Parser with specified options can&#39;t be built&nbsp;&nbsp;pce.printStackTrace();</code>} catch (IOException ioe) {&nbsp;&nbsp;...<a name="wp64817"> </a></pre></div><a name="wp64818"> </a><p class="pBody">You're done! You have now created a transformer which will use a <code class="cCode">SAXSource</code> as input, and produce a <code class="cCode">StreamResult</code> as output. </p><a name="wp64820"> </a><h3 class="pHeading2">Doing the Conversion</h3><a name="wp64821"> </a><p class="pBody">Now run the application on the address book file. Your output should look like this:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;addressbook&gt;&nbsp;&nbsp;&lt;nickname&gt;Fred&lt;/nickname&gt;&nbsp;&nbsp;&lt;email&gt;fred@barneys.house&lt;/email&gt;&nbsp;&nbsp;&lt;html&gt;TRUE&lt;/html&gt;&nbsp;&nbsp;&lt;firstname&gt;Fred&lt;/firstname&gt;&nbsp;&nbsp;&lt;lastname&gt;Flintstone&lt;/lastname&gt;&nbsp;&nbsp;&lt;work&gt;999-Quarry&lt;/work&gt;&nbsp;&nbsp;&lt;home&gt;999-BedrockLane&lt;/home&gt;&nbsp;&nbsp;&lt;fax&gt;888-Squawk&lt;/fax&gt;&nbsp;&nbsp;&lt;pager&gt;777-pager&lt;/pager&gt;&nbsp;&nbsp;&lt;cell&gt;555-cell&lt;/cell&gt;&lt;/addressbook&gt;<a name="wp64822"> </a></pre></div><a name="wp64823"> </a><p class="pBody">You have now successfully converted an existing data structure to XML. And it wasn't even that hard. Congratulations! </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="JAXPXSLT4.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="JAXPXSLT6.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 + -