📄 jaxpxslt8.html
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Concatenating Transformations with a Filter Chain</title> <link rel="StyleSheet" href="document.css" type="text/css" media="all" /> <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" /> <link rel="Table of Contents" href="J2EETutorialTOC.html" /> <link rel="Previous" href="JAXPXSLT7.html" /> <link rel="Next" href="JAXPXSLT9.html" /> <link rel="Index" href="J2EETutorialIX.html" /> </head> <body> <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="JAXPXSLT7.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="JAXPXSLT9.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"> <blockquote><a name="wp72462"> </a><h2 class="pHeading1">Concatenating Transformations with a Filter Chain</h2><a name="wp65401"> </a><p class="pBody">It is sometimes useful to create a <span style="font-style: italic">filter chain</span> -- a concatenation of XSLT transformations in which the output of one transformation becomes the input of the next. This section of the tutorial shows you how to do that. </p><a name="wp65403"> </a><h3 class="pHeading2">Writing the Program</h3><a name="wp65404"> </a><p class="pBody">Start by writing a program to do the filtering. This example will show the full source code, but you can use one of the programs you've been working on as a basis, to make things easier.</p><hr><a name="wp65405"> </a><p class="pNote">Note: The code described here is contained in <code class="cCode"><a href="../examples/jaxp/xslt/samples/FilterChain.java" target="_blank">FilterChain.java</a></code>.</p><hr><a name="wp65406"> </a><p class="pBody">The sample program includes the import statements that identify the package locations for each class:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">import javax.xml.parsers.FactoryConfigurationError;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.InputSource;import org.xml.sax.XMLReader;import org.xml.sax.XMLFilter;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.sax.SAXTransformerFactory;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.sax.SAXResult;import javax.xml.transform.stream.StreamSource;import javax.xml.transform.stream.StreamResult;import java.io.*;<a name="wp65407"> </a></pre></div><a name="wp65408"> </a><p class="pBody">The program also includes the standard error handlers you're used to. They're listed here, just so they are all gathered together in one place:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">}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();}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();}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();}catch (IOException ioe) { // I/O error ioe.printStackTrace();}<a name="wp65409"> </a></pre></div><a name="wp65410"> </a><p class="pBody">In between the import statements and the error handling, the core of the program consists of the code shown below.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public static void main (String argv[]){ if (argv.length != 3) { System.err.println ( "Usage: java FilterChain style1 style2 xmlfile"); System.exit (1); } try { // Read the arguments File stylesheet1 = new File(argv[0]); File stylesheet2 = new File(argv[1]); File datafile = new File(argv[2]); // Set up the input stream BufferedInputStream bis = new BufferedInputStream(newFileInputStream(datafile)); InputSource input = new InputSource(bis); // Set up to read the input file <code class="cCodeBold">(see Note #1)</code> SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); SAXParser parser = spf.newSAXParser(); XMLReader reader = parser.getXMLReader(); // Create the filters <code class="cCodeBold">(see Note #2)</code> SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance(); XMLFilter filter1 = stf.newXMLFilter( new StreamSource(stylesheet1)); XMLFilter filter2 = stf.newXMLFilter( new StreamSource(stylesheet2)); // Wire the output of the reader to filter1 <code class="cCodeBold">(see Note #3)</code> // and the output of filter1 to filter2 filter1.setParent(reader); filter2.setParent(filter1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -