Source Code : Applies a stylesheet (that receives parameters) to a given xml document.
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Applies a stylesheet (that receives parameters) to a given xml document.
/**
* 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/
*/
/**
* This class provides a set of static methods to load and transform XML
* documents. It supports parameter-aware stylesheets (XSLT).
*
* @author Miguel Ferreira
*
*/
import java.io.StringWriter;
import java.util.Map;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
public class Util{
/**
* Applies a stylesheet (that receives parameters) to a given xml document.
* The resulting XML document is converted to a string after transformation.
*
* @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 as a string
* @throws Exception
*/
public static String transformDocumentAsString(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());
}
}
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
// Perform the transformation.
transformer.transform(new DOMSource(xmlDocument), streamResult);
// Now you can get the output Node from the DOMResult.
return stringWriter.toString();
}
}
Thank with us