Created
October 16, 2012 06:49
-
-
Save liudong/3897605 to your computer and use it in GitHub Desktop.
Java: apply XSLT
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
private String applyXslt(String xsltString, String xmlToLift, String lodUri) throws ParserConfigurationException, SAXException, IOException, TransformerException { | |
// create transformer | |
Transformer xsltTransformer = HtmlTransformerFactory.createXsltTransformerByContent(xsltString); | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder parser = factory.newDocumentBuilder(); | |
Document document = parser.parse(new ByteArrayInputStream(xmlToLift.getBytes()), null); | |
DOMSource source = new DOMSource(document); | |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | |
StreamResult result = new StreamResult(bout); | |
if ( lodUri != null ) { | |
xsltTransformer.setParameter("lodUri", lodUri); | |
} | |
xsltTransformer.transform(source, result); | |
xsltTransformer.setOutputProperty("encoding", "UTF-8"); | |
if (result.getOutputStream() != null) { | |
String resultString = result.getOutputStream().toString(); | |
return resultString; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment