Skip to content

Instantly share code, notes, and snippets.

@tkrisztian95
Created July 2, 2019 09:48
Show Gist options
  • Save tkrisztian95/3952429c1b0c55b51ba61562ac450e9c to your computer and use it in GitHub Desktop.
Save tkrisztian95/3952429c1b0c55b51ba61562ac450e9c to your computer and use it in GitHub Desktop.
Transform XML string by XSLT
public class XmlUtils {
public static String XsltTransform(String xml, String xslt) throws TransformerException {
Source xsltSource = new StreamSource(new StringReader(xslt));
Source xmlSource = new StreamSource(new StringReader(xml));
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xsltSource);
//create a StringWriter for the output
StringWriter outWriter = new StringWriter();
StreamResult result = new StreamResult(outWriter);
transformer.transform(xmlSource, result);
StringBuffer sb = outWriter.getBuffer();
String transformedXml = sb.toString();
return transformedXml;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment