Created
August 30, 2010 11:58
-
-
Save jankronquist/557322 to your computer and use it in GitHub Desktop.
SimpleXslt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.StringReader; | |
import java.io.StringWriter; | |
import javax.xml.transform.Templates; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.stream.StreamResult; | |
import javax.xml.transform.stream.StreamSource; | |
public class SimpleXslt { | |
private Templates templates; | |
public SimpleXslt(String name) { | |
try { | |
templates = TransformerFactory.newInstance().newTemplates(new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(name))); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public String transform(String xml) { | |
try { | |
StringWriter result = new StringWriter(); | |
templates.newTransformer().transform(new StreamSource(new StringReader(xml)), new StreamResult(result)); | |
return result.toString(); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment