java xml xslt_Java中使用XML字符串和XSLT字符串作为参数获取转换后的字符串,支持xsl:include、xsl:import...

本文介绍了如何使用Java进行XML字符串与XSLT的转换,展示了调用invokeXmlstrAndXslstr方法的实例,并处理了异常。核心内容包括TransformerFactory的配置和使用,处理XML和XSLT字符串,以及输出转换后的结果。
摘要由CSDN通过智能技术生成

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值