📄 jaxpxslt4.html
字号:
</p><hr><a name="wp85640"> </a><p class="pBody">Finally, add the code highlighted below to catch the new errors that can be generated:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeBold">} catch (TransformerConfigurationException tce) { // Error generated by the parser System.out.println ("* Transformer Factory error"); System.out.println(" " + tce.getMessage() ); // Use the contained exception, if any Throwable x = tce; if (tce.getException() != null) x = tce.getException(); x.printStackTrace();</code><a name="wp64626"> </a><code class="cCodeBold">} catch (TransformerException te) { // Error generated by the parser System.out.println ("* Transformation error"); System.out.println(" " + te.getMessage() ); // Use the contained exception, if any Throwable x = te; if (te.getException() != null) x = te.getException(); x.printStackTrace();</code>} catch (SAXParseException spe) { ...<a name="wp64627"> </a></pre></div><a name="wp64628"> </a><p class="pBody">Notes:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp64629"> </a><div class="pSmartList1"><li><code class="cCode">TransformerExceptions</code> are thrown by the transformer object.</li></div><a name="wp79126"> </a><div class="pSmartList1"><li><code class="cCode">TransformerConfigurationExceptions</code> are thrown by the factory.</li></div><a name="wp79127"> </a><div class="pSmartList1"><li>To preserve the XML document's <code class="cCode">DOCTYPE</code> setting, it is also necessary to add the following code:</li></div><a name="wp86033"> </a><p class="pBodyRelative"><code class="cCode">import javax.xml.transform.OutputKeys;<br />...<br />if (document.getDoctype() != null){<br /> String systemValue = (new<br />   File(document.getDoctype().getSystemId())).getName();<br /> transformer.setOutputProperty(<br />  OutputKeys.DOCTYPE_SYSTEM, systemValue<br /> );<br />}</code></p></ul></div><a name="wp64635"> </a><h3 class="pHeading2">Writing the XML</h3><a name="wp64638"> </a><p class="pBody">For instructions on how to compile and run the program, see <a href="JAXPSAX3.html#wp64274">Compiling and Running the Program</a> from the SAX tutorial. (If you're working along, substitute "TransformationApp" for "Echo" as the name of the program. If you are compiling the sample code, use "TransformationApp02".) When you run the program on <code class="cCode">slideSample01.xml</code>, this is the output you see:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version="1.0" encoding="UTF-8"?><!-- A SAMPLE set of slides --><slideshow author="Yours Truly" date="Date of publication"title="Sample Slide Show"> <!-- 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><a name="wp64643"> </a></pre></div><hr><a name="wp64647"> </a><p class="pNote">Note: The order of the attributes may vary, depending on which parser you are using.</p><hr><a name="wp66785"> </a><p class="pBody">To find out more about configuring the factory and handling validation errors, see <a href="JAXPDOM3.html#wp68274">Reading XML Data into a DOM</a>, <a href="JAXPDOM3.html#wp64106">Additional Information</a>.</p><a name="wp64649"> </a><h3 class="pHeading2">Writing Out a Subtree of the DOM</h3><a name="wp64650"> </a><p class="pBody">It is also possible to operate on a subtree of a DOM. In this section of the tutorial, you'll experiment with that option.</p><hr><a name="wp64651"> </a><p class="pNote">Note: The code discussed in this section is in <code class="cCode"><a href="../examples/jaxp/xslt/samples/TransformationApp03.java" target="_blank">TransformationApp03.java</a></code>. The output is in <code class="cCode"><a href="../examples/jaxp/xslt/samples/TransformationLog03.txt" target="_blank">TransformationLog03.txt</a></code>. (The browsable version is <code class="cCode"><a href="../examples/jaxp/xslt/samples/TransformationLog03.html" target="_blank">TransformationLog03.html</a></code>.)</p><hr><a name="wp64652"> </a><p class="pBody">The only difference in the process is that now you will create a <code class="cCode">DOMSource</code> using a node in the DOM, rather than the entire DOM. The first step will be to import the classes you need to get the node you want. Add the code highlighted below to do that:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><span style="font-weight: normal">import org.w3c.dom.Document;import org.w3c.dom.DOMException;</span><code class="cCodeBold">import org.w3c.dom.Node;import org.w3c.dom.NodeList; </code><a name="wp64653"> </a></pre></div><a name="wp64654"> </a><p class="pBody">The next step is to find a good node for the experiment. Add the code highlighted below to select the first <code class="cCode"><slide></code> element:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">try { File f = new File(argv[0]); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(f);<code class="cCodeBold"> // Get the first <slide> element in the DOM NodeList list = document.getElementsByTagName("slide"); Node node = list.item(0);</code><a name="wp64655"> </a></pre></div><a name="wp64656"> </a><p class="pBody">Finally, make the changes shown below to construct a source object that consists of the subtree rooted at that node:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><code class="cCodeStruck">DOMSource source = new DOMSource(document);</code><code class="cCodeBold">DOMSource source = new DOMSource(node);</code>StreamResult result = new StreamResult(System.out);transformer.transform(source, result);<a name="wp64657"> </a></pre></div><a name="wp64658"> </a><p class="pBody">Now run the app. Your output should look like this: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><?xml version="1.0" encoding="UTF-8"?><slide type="all"> <title>Wake up to WonderWidgets!</title> </slide><a name="wp64659"> </a></pre></div><a name="wp64660"> </a><h4 class="pHeading3">Clean Up</h4><a name="wp64661"> </a><p class="pBody">Because it will be easiest to do now, make the changes shown below to back out the additions you made in this section. (<code class="cCode"><a href="../examples/jaxp/xslt/samples/TransformationApp04.java" target="_blank">TransformationApp04.java</a></code> contains these changes.)</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">Import org.w3c.dom.DOMException;<code class="cCodeStruck">import org.w3c.dom.Node;import org.w3c.dom.NodeList; </code>... try { ... <code class="cCodeStruck">// Get the first <slide> element in the DOM NodeList list = document.getElementsByTagName("slide"); Node node = list.item(0);</code> ... <code class="cCodeStruck">DOMSource source = new DOMSource(node);</code> StreamResult result = new StreamResult(System.out); transformer.transform(source, result);<a name="wp64662"> </a></pre></div><a name="wp64664"> </a><h3 class="pHeading2">Summary</h3><a name="wp64665"> </a><p class="pBody">At this point, you've seen how to use a transformer to write out a DOM, and how to use a subtree of a DOM as the source object in a transformation. In the next section, you'll see how to use a transformer to create XML from any data structure you are capable of parsing.</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="JAXPXSLT3.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="JAXPXSLT5.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 + -