Java tutorial
//package com.java2s; /* * The following methods come from a library written by Tom Fennelly. * Here was the header of the licence. */ import java.io.IOException; import java.io.StringWriter; import java.lang.reflect.Constructor; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.URIResolver; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamResult; public class Main { public static String applyXslToDocument(Source xslt, Source doc, URIResolver resolver, Properties transformerProperties, HashMap<String, String> params) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchMethodException, TransformerConfigurationException, TransformerException { return (applyXslToDocument(xslt, doc, resolver, transformerProperties, params, null)); } public static String applyXslToDocument(Source xslt, Source doc, URIResolver resolver, Properties transformerProperties, HashMap<String, String> params, String transformerClassName) throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException, NoSuchMethodException, TransformerConfigurationException, TransformerException { TransformerFactory transformerFactory = null; if (transformerClassName == null) transformerFactory = TransformerFactory.newInstance(); else { Class transformerClass = Class.forName(transformerClassName); Constructor defaultConstructor = transformerClass.getConstructor(null); transformerFactory = (TransformerFactory) transformerClass.newInstance(); } if (resolver != null) transformerFactory.setURIResolver(resolver); Transformer transformer = transformerFactory.newTransformer(xslt); if (transformerFactory != null) transformer.setOutputProperties(transformerProperties); if (params != null) { for (Map.Entry<String, String> cursor : params.entrySet()) { transformer.setParameter(cursor.getKey(), cursor.getValue()); } } StringWriter strWriter = new StringWriter(); StreamResult result = new StreamResult(strWriter); transformer.transform(doc, result); return (strWriter.toString()); } }