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

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

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

引入模板引擎的依赖:

① 在开发环境下,可通过如下方式进行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<String, XsltExecutable> 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 class SaxTransform {

    private static Processor proc;

    private static HashMap<String, XsltExecutable> xslCache = new HashMap();

    static {
        proc = new Processor(false);
    }

    public static String xslTransform(String strXml, Resource xslFile) {
        Serializer out = 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 = new StreamSource(xslFile.getInputStream());
                XsltCompiler comp = proc.newXsltCompiler();
                exp = comp.compile(xslStream);
                xslCache.put(xslFile.getURL().getPath(), exp);
            }
            xmlReader = new StringReader(strXml);
            StreamSource xmlStream = new StreamSource(xmlReader);
            out = proc.newSerializer();
            out.setOutputWriter(writer);
            trans = exp.load();
            trans.setSource(xmlStream);
            trans.setDestination(out);
            trans.transform();
            String result = writer.toString();
            return result;
        } 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;
    }
}
 
 

猜你喜欢

转载自www.cnblogs.com/slimshady/p/11307119.html
xsl
今日推荐