Java tutorial
//package com.java2s; import java.io.StringWriter; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class Main { /** * <p>Use this to grab things like the Plays title, or the characters in the play.</p> * <p>You will have to specify the XSLT file to do this</p> * * @param xmlFile the name and path of the xml file * @param xslFile the name and path of the xslt file *@param delim the delimiter you wish to split with. * @return A string delimited of the transformed document * @see MimeConstants */ public static String simpleTransformToStr(String xmlFile, String xslFile) { // -------------------------------------------------------------------- // http://www.oreillynet.com/pub/a/oreilly/java/news/javaxslt_0801.html // -------------------------------------------------------------------- String returnValue = ""; try { // JAXP reads data using the Source interface Source xmlSource = new StreamSource(xmlFile); Source xsltSource = new StreamSource(xslFile); // the factory pattern supports different XSLT processors TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans; trans = transFact.newTransformer(xsltSource); // ---------------------------------------------------------- // http://forum.java.sun.com/thread.jspa?threadID=636335&messageID=3709470 // Paul, you had alot of trouble figuring out how to pass the output of the transformation, the url above is hwere you got the following chunk // ---------------------------------------------------------- StringWriter output = new StringWriter(); trans.transform(xmlSource, new StreamResult(output)); return output.toString(); } catch (TransformerException ex) { ex.printStackTrace(); } return null; } }