Here you can find the source of transformDocument(Document xmlDocument, String xsltFilename)
Parameter | Description |
---|---|
xmlDocument | the xml document to be transformed |
xsltFilename | the filename of the stylesheet |
Parameter | Description |
---|---|
Exception | an exception |
public static Document transformDocument(Document xmlDocument, String xsltFilename) throws Exception
//package com.java2s; /**/*from w w w. ja v a 2 s .com*/ * The contents of this file are subject to the license and copyright * detailed in the LICENSE and NOTICE files at the root of the source * tree and available online at * * http://www.dspace.org/license/ */ import java.util.Hashtable; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Document; public class Main { /** * Applies a stylesheet to a given xml document. * * @param xmlDocument * the xml document to be transformed * @param xsltFilename * the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static Document transformDocument(Document xmlDocument, String xsltFilename) throws Exception { return transformDocument(xmlDocument, new Hashtable(), xsltFilename); } /** * Applies a stylesheet (that receives parameters) to a given xml document. * * @param xmlDocument * the xml document to be transformed * @param parameters * the hashtable with the parameters to be passed to the * stylesheet * @param xsltFilename * the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static Document transformDocument(Document xmlDocument, Map<String, String> parameters, String xsltFilename) throws Exception { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance() .newTransformer(new StreamSource(xsltFilename)); // set transformation parameters if (parameters != null) { for (Map.Entry<String, String> param : parameters.entrySet()) { transformer.setParameter(param.getKey(), param.getValue()); } } // Create an empty DOMResult object for the output. DocumentBuilderFactory dFactory = DocumentBuilderFactory .newInstance(); dFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); Document dstDocument = dBuilder.newDocument(); DOMResult domResult = new DOMResult(dstDocument); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), domResult); // Now you can get the output Node from the DOMResult. return dstDocument; } }