📄 jaxpxslt5.html
字号:
{ 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"> // Global value so it can be ref'd by the tree-adapter static Document document; </code><a name="wp64810"> </a> public static void main(String argv[]) { ... <code class="cCodeStruck">DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();</code> <code class="cCodeStruck">//factory.setNamespaceAware(true); //factory.setValidating(true); </code> // Create the sax "parser".<code class="cCodeBold"> AddressBookReader saxReader = new AddressBookReader();</code> try { File f = new File(argv[0]); <code class="cCodeStruck"> DocumentBuilder builder = factory.newDocumentBuilder(); 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) { // Error generated by the parser System.out.println("\n** Parsing error" + ", line " + spe.getLineNumber() + ", uri " + spe.getSystemId()); System.out.println(" " + spe.getMessage() ); // Use the contained exception, if any Exception x = spe; if (spe.getException() != null) x = spe.getException(); x.printStackTrace();} catch (SAXException sxe) { // Error generated by this application // (or a parser-initialization error) Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace();} catch (ParserConfigurationException pce) { // Parser with specified options can't be built pce.printStackTrace();</code>} catch (IOException ioe) { ...<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"><?xml version="1.0" encoding="UTF-8"?><addressbook> <nickname>Fred</nickname> <email>fred@barneys.house</email> <html>TRUE</html> <firstname>Fred</firstname> <lastname>Flintstone</lastname> <work>999-Quarry</work> <home>999-BedrockLane</home> <fax>888-Squawk</fax> <pager>777-pager</pager> <cell>555-cell</cell></addressbook><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 + -