java xsl转换xml文件_springboot下xml文件通过xsl模板转换的问题

springboot项目下的xsl文件路径如下:

ca80dd650f095b3296110a53b886e585.png

项目需求: 需要将 a.xml 通过 xsl模板 转换成 b.xml

引入模板引擎的依赖:

141e8c4b08006a85b35baf4f36f8382b.png

① 在开发环境下,可通过如下方式进行xml间的转换

File cXslFile = ResourceUtils.getFile("classpath:templete/15/04/PolicyQueryRequest.xsl");

Reader reader=SaxTransform.readDom(a.xml);

String b.xml= SaxTransform.transform(reader, cXslFile);

SaxTransform.java

@Slf4j

public class SaxTransform {

private static Processor proc;

private static HashMap xslCache = new HashMap();

static {

proc = new Processor(false);

}

public static String transform(Reader xmlReader, File xslFile) {

Serializer out = null;

StringWriter writer = new StringWriter();

XsltTransformer trans = null;

try {

XsltCompiler comp = proc.newXsltCompiler();

XsltExecutable exp = xslCache.get(xslFile);

if(exp==null){

StreamSource xslStream = new StreamSource(xslFile);

exp = comp.compile(xslStream);

xslCache.put(xslFile.getPath(),exp);

}

StreamSource xmlStream = new StreamSource(xmlReader);

XdmNode source = proc.newDocumentBuilder().build(xmlStream);

out = proc.newSerializer();

out.setOutputWriter(writer);

trans = exp.load();

trans.setInitialContextNode(source);

trans.setDestination(out);

trans.transform();

String result = writer.toString();

return result;

} catch (SaxonApiException sae) {

log.error("XML报文转化失败!" + sae.getMessage(), sae);

try {

xmlReader.close();

out.close();

writer.close();

trans.close();

} catch (Exception e) {

}

} finally {

try {

xmlReader.close();

out.close();

writer.close();

trans.close();

} catch (Exception e) {

}

}

return null;

}

public static Reader readDom(String doc){

return new StringReader(doc);

}

}

②当项目打成jar包放到服务器,无法获取jar包里的xsl文件,需要以流的形式获取jar包里的文件

Resource resource=new DefaultResourceLoader().getResource("classpath:templete/15/04/PolicyQueryRequest.xsl");

String b.xml= SaxTransform.xslTransform(a.xml, resource);

SaxTransform.java

public classSaxTransform {private staticProcessor proc;private static HashMap xslCache = newHashMap();static{

proc= new Processor(false);

}public staticString xslTransform(String strXml, Resource xslFile) {

Serializerout = null;

StringWriter writer= new StringWriter(1024);

XsltTransformer trans= null;

StringReader xmlReader= null;try{

XsltExecutable exp= xslCache.get(xslFile.getURL().getPath());if (exp == null) {

StreamSource xslStream= newStreamSource(xslFile.getInputStream());

XsltCompiler comp=proc.newXsltCompiler();

exp=comp.compile(xslStream);

xslCache.put(xslFile.getURL().getPath(), exp);

}

xmlReader= newStringReader(strXml);

StreamSource xmlStream= newStreamSource(xmlReader);out =proc.newSerializer();out.setOutputWriter(writer);

trans=exp.load();

trans.setSource(xmlStream);

trans.setDestination(out);

trans.transform();

String result=writer.toString();returnresult;

}catch(FileNotFoundException sae) {try{

xmlReader.close();out.close();

writer.close();

trans.close();

}catch(Exception e) {

}

}catch(Exception sae) {try{

xmlReader.close();out.close();

writer.close();

trans.close();

}catch(Exception e) {

}

}finally{try{out.close();

writer.close();

trans.close();

}catch(Exception e) {

}

}return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值