Skip to content

Instantly share code, notes, and snippets.

@jankronquist
Created August 30, 2010 11:58
Show Gist options
  • Save jankronquist/557322 to your computer and use it in GitHub Desktop.
Save jankronquist/557322 to your computer and use it in GitHub Desktop.
SimpleXslt
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