【问题标题】:How can I pass the XSLT file location which is in my package in java to Transformer?如何将我的 java 包中的 XSLT 文件位置传递给 Transformer?
【发布时间】:2017-09-26 05:15:30
【问题描述】:

我的 xslt 文件位于包结构 com/test/application/meta/xslt/forgotPassword.xslt

TransformerFactory tFactory = TransformerFactory.newInstance();
    Source xslDoc = new StreamSource("/com/test/application/meta/xslt/forgotPassword.xslt");
    Source source = new StreamSource(new StringReader(getXmlData()));
    try {
        Transformer transformer = tFactory.newTransformer(xslDoc);
        StringWriter writer = new StringWriter();
        transformer.transform(source, new StreamResult(writer));
        String output = writer.toString();
        return output;
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

出现错误:

FATAL ERROR:  '/com/test/application/meta/xslt/forgotPassword.xslt (No such file or directory)'

当我尝试使用时

Source xslDoc = new StreamSource("com/test/application/meta/xslt/forgotPassword.xslt");

我收到此错误:

FATAL ERROR:  '/var/lib/tomcat7/com/test/application/meta/xslt/forgotPassword.xslt (No such file or directory)'

如何将正确的文件位置传递给 Transformer ?该文件位于 com/test/application/meta/xslt/forgotPassword.xslt

包中

谢谢

【问题讨论】:

    标签: java xml xslt path filepath


    【解决方案1】:

    你能把你的 xml 文件移动到 src/main/resources 吗?或者它以某种方式自动生成?

    如果您可以将其移动到资源文件夹,该代码可以帮助您完成这项工作

     InputStream stream = YourClassName.class.getResourceAsStream("/xslt/forgotPassword.xslt");
     Source schemaSource = new StreamSource(stream);
    

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 1970-01-01
      • 2017-01-10
      • 2011-05-17
      相关资源
      最近更新 更多