/** * Transform the input document * * @param xml The input xml * @return The rendered HTML * @throws IOException */ public Document transform(Document document) throws IOException { String renderedHTML = ""; javax.xml.transform.Transformer transformer; Document transformedDoc = null; try { transformer = m_template.newTransformer(); assignParameters(transformer, m_params); DocumentSource fileSource = new DocumentSource(document); // Source fileSource = new StreamSource(xml); // Result result = new StreamResult(renderedHTML); DocumentResult result = new DocumentResult(); transformer.transform(fileSource, result); transformedDoc = result.getDocument(); } catch (TransformerConfigurationException e) { throw new IOException(e.getMessage()); } catch (TransformerException e) { throw new IOException(e.getMessage()); } return transformedDoc; }
public Document styleDocument(Document document, String stylesheet) throws Exception { // load the transformer using JAXP TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(stylesheet)); // now lets style the given document DocumentSource source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); // return the transformed document Document transformedDoc = result.getDocument(); return transformedDoc; }
/** Perform XSLT on the stylesheet */ protected void process(Document document) throws Exception { // load the transformer TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xsl)); // now lets create the TrAX source and result // objects and do the transformation Source source = new DocumentSource(document); DocumentResult result = new DocumentResult(); transformer.transform(source, result); // output the transformed document Document transformedDoc = result.getDocument(); writer.write(transformedDoc); }
// 使用XSLT模板对Document进行转化 public static Document getStyleDocument(Document document, String xsltfile) throws Exception { InputStream in = null; if (CommonPath.path == null || CommonPath.path.equals("")) { in = ClassLoader.getSystemResourceAsStream(xsltfile); } else { String[] ss = xsltfile.split("/"); String filename = ss[ss.length - 1]; File file = new File(CommonPath.path + filename); in = new FileInputStream(file); } DocumentSource docSource = new DocumentSource(document); DocumentResult docResult = new DocumentResult(); TransformerFactory factory = TransformerFactory.newInstance(); Templates cachedXSLT = factory.newTemplates(new StreamSource(in)); Transformer transformer = cachedXSLT.newTransformer(); if (transformer != null) { transformer.transform(docSource, docResult); } Document resultDoc = docResult.getDocument(); return resultDoc; }