packageTest;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.PrintWriter;importjava.io.StringReader;importjava.net.URLDecoder;importjavax.xml.transform.Source;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerException;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.URIResolver;importjavax.xml.transform.sax.SAXSource;importjavax.xml.transform.stream.StreamResult;importjavax.xml.transform.stream.StreamSource;importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importorg.xml.sax.InputSource;public classTest {private static final Logger logger = LogManager.getLogger(Test.class);private static final TransformerFactory tFactory =getTransformerFactory();public static voidmain(String[] args) {try{
String xmlStr="\r\n" +
" C5CAB5D6-E8E8-4E90-8FE2-2CCDCB4FE562\r\n" +
" <_>1579165992522\r\n" +
"";
String xsltStr="" +
"columnId=" +
"";
String resultStr=invokeXmlstrAndXslstr(xmlStr, xsltStr);
System.out.println("xmlStr:");
System.out.println(xmlStr);
System.out.println("xsltStr:");
System.out.println(xsltStr);
System.out.println("resultStr:");
System.out.println(resultStr);
}catch(Exception e) {
e.printStackTrace();
}
}public static String invokeXmlstrAndXslstr(String xmlString, String xslString) throwsException {
String returnXMLString= "";
Transformer transformer;
ByteArrayOutputStream bytes= newByteArrayOutputStream();
PrintWriter printWriter;
StreamSource source;
StreamResult result;//补全为XSLT字符串 根据实际应用决定是否需要这部分补全字符串的代码
xslString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ ""
+ ""
+ " " + xslString + " " + "";
transformer= tFactory.newTransformer(new SAXSource(new InputSource(newStringReader(xslString))));
source= new StreamSource(newStringReader(xmlString));
bytes= newByteArrayOutputStream();
printWriter= newPrintWriter(bytes);
result= new StreamResult(printWriter);//存放中间结果//使用指定的样式表,转换XML文档
transformer.transform(source, result);if (bytes != null) {
returnXMLString=bytes.toString();
}returnreturnXMLString;
}public staticTransformerFactory getTransformerFactory() {
TransformerFactory tFactoryObj=TransformerFactory.newInstance();
tFactoryObj.setURIResolver(newURIResolver() {public Source resolve(String href, String base) throwsTransformerException {
StreamSource ss= null;try{
ss=getXSL(href);
}catch(Exception e) {try{throwe;
}catch(Exception e1) {
logger.error("获取include的XSLT文件异常!异常信息:", e1);
}
}returnss;
}
});returntFactoryObj;
}/*** 指定XSLT内部include或import的XSLT文件的完整路劲 该方法需要根据项目情况调整指定路劲的规则
*
*@paramhref include或import标签中href属性的值
*@returninclude或import的XSLT文件的完整路劲
*@throwsException*/
public static StreamSource getXSL(String href) throwsException {
StreamSource ss;
String pathVal= Test.class.getResource("/").toString().replace("file:/", "/").replace("/WEB-INF/classes/", "/")+href;
pathVal= pathVal.replaceAll("//", "/");
pathVal= URLDecoder.decode(pathVal, "utf-8");
File f= newFile(pathVal);if(f.exists()) {
ss= new StreamSource(newFileInputStream(f));
}else{
f= new File("src/main/resources/xslt/" +href);if(f.exists()) {
ss= new StreamSource(newFileInputStream(f));
}else{
ss= new StreamSource(Test.class.getResource("/xslt/" +href).openStream());
}
}returnss;
}
}