阅读更多
public String mergeXml(String xml, String xslt) {
if (LOG.isDebugEnabled()) {
LOG.debug("xml:" + xml + "\n xsl:" + xslt);
}
ByteArrayOutputStream out = null;
try {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Source xslSource = new StreamSource(new StringReader(xslt));
Templates templates = transformerFactory.newTemplates(xslSource);
Transformer transformer = templates.newTransformer();
Source xmlSource = new StreamSource(new StringReader(xml));
out = new ByteArrayOutputStream();
Result outputTarget = new StreamResult(out);
transformer.transform(xmlSource, outputTarget);
return new String(out.toByteArray(), "utf-8");
} catch (TransformerConfigurationException e) {
LOG.warn("合并错xml:" + xml + "\n xsl:" + xslt + e);
} catch (TransformerException e) {
LOG.warn("合并错xml:" + xml + "\n xsl:" + xslt + e);
} catch (UnsupportedEncodingException e) {
LOG.warn("合并时编码输出错" + xml + "\n xsl:" + xslt + e);
} catch (NullPointerException e) {
LOG.warn(e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOG.warn("连接关闭发生错误");
}
}
}
return null;
}