从 JAR 文件加载 XSLT 文件会加载 JAR 文件本身而不是 XSLT

发布于 2024-10-04 17:50:56 字数 466 浏览 0 评论 0原文

我在 JAR 文件内的类路径上有一个 XSLT 文件。我尝试使用 InputStream 加载 XSLT 文件。调试后,InputStream 包含 JAR 文件而不是 XSLT 文件。

String xslPath = "/com/japi/application/templates/foo.xslt";
InputStream is = getClass().getResourceAsStream(xslPath);
...
Source xslt = new StreamSource(is);
trans = factory.newTransformer(xsltSource); //Fatal error. Error parsing XSLT {0}

我已仔细检查 XSLT 文件的路径是否正确,并且 JAR 文件中包含物理文件。有什么想法吗?

I've an XSLT file on the classpath inside a JAR file. I've tried to load the XSLT file using an InputStream. After debugging, the InputStream contains the JAR file instead of the XSLT file.

String xslPath = "/com/japi/application/templates/foo.xslt";
InputStream is = getClass().getResourceAsStream(xslPath);
...
Source xslt = new StreamSource(is);
trans = factory.newTransformer(xsltSource); //Fatal error. Error parsing XSLT {0}

I have double checked that the path to the XSLT file is correct and a physical file is included in the JAR file. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不羁少年 2024-10-11 17:50:56

创建自定义解析器以从类路径解析
将其设置为 Transfromer
为了测试我在 Eclipse 项目的类路径中设置了一个 jar 文件,

所有代码都在下面
----- 运行示例-------------
`

public class RunTransform {

    public static void main(String[] args) {

        //  SimpleTransform.transform("xsl/SentAdapter.xsl", "C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/acc.xml");

        SimpleTransform.transform("xslt/ibanvalidation/accuity-ibanvalidationresponse.xsl", "C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/acc.xml");
    }

}

<代码>
-----------示例转换示例 ----------------

package com;

import java.io.File;
import java.io.FileOutputStream;


import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;


public class SimpleTransform {

    public static void transform(String xslName,String xmlName) {
        try {  
            ResourceResolver resloader = new ResourceResolver();
              TransformerFactory tFactory = TransformerFactory.newInstance();
              tFactory.setURIResolver(resloader);

              StreamSource xsltSRC = new StreamSource(resloader.resolve(xslName));

              Transformer transformer = tFactory.newTransformer(xsltSRC);

              StreamSource xmlSSRC = new StreamSource(xmlName);
              System.out.println("Streamm sources created .....");

              System.out.println("XSLT SET ....");
              transformer.transform(xmlSSRC, new StreamResult(new FileOutputStream(new File("C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/result.xml"))));
              System.out.println("Finished transofrmation ..........");
              System.out.println("************* The result is out in respoinse *************");


             } catch (Throwable t) {
                      t.printStackTrace();
             }

    }

}

`


-----------自定义解析器代码 ---------------
`

package com;

import javax.xml.transform.URIResolver;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;

public class ResourceResolver implements URIResolver {

    /* (non-Javadoc)
     * @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)
     */
    public Source resolve(String href, String base) throws TransformerException {

        try {

            InputStream is = ClassLoader.getSystemResourceAsStream(href);
            return new StreamSource(is, href);
        } // try
        catch (Exception ex) {
            throw new TransformerException(ex);
        } // catch
    } // resolve

    /**
     * @param href
     * @return
     * @throws TransformerException
     */
    public InputStream resolve(String href) throws TransformerException {
        try {

            InputStream is = ClassLoader.getSystemResourceAsStream(href);
            return is;
        } // try
        catch (Exception ex) {
            throw new TransformerException(ex);
        } // catch
    }
} // ResourceResolver

`

Create a Custom Resolver to resolve from class path
set that to Transfromer
to test i had a jar file set in classpath in eclipse project

all code is here below
----- run example -------------
`

public class RunTransform {

    public static void main(String[] args) {

        //  SimpleTransform.transform("xsl/SentAdapter.xsl", "C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/acc.xml");

        SimpleTransform.transform("xslt/ibanvalidation/accuity-ibanvalidationresponse.xsl", "C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/acc.xml");
    }

}


-----------Sample transfomring example ----------------

package com;

import java.io.File;
import java.io.FileOutputStream;


import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;


public class SimpleTransform {

    public static void transform(String xslName,String xmlName) {
        try {  
            ResourceResolver resloader = new ResourceResolver();
              TransformerFactory tFactory = TransformerFactory.newInstance();
              tFactory.setURIResolver(resloader);

              StreamSource xsltSRC = new StreamSource(resloader.resolve(xslName));

              Transformer transformer = tFactory.newTransformer(xsltSRC);

              StreamSource xmlSSRC = new StreamSource(xmlName);
              System.out.println("Streamm sources created .....");

              System.out.println("XSLT SET ....");
              transformer.transform(xmlSSRC, new StreamResult(new FileOutputStream(new File("C:/Amin/AllWorkspaces/ProtoTypes/XsltDemo/xml/result.xml"))));
              System.out.println("Finished transofrmation ..........");
              System.out.println("************* The result is out in respoinse *************");


             } catch (Throwable t) {
                      t.printStackTrace();
             }

    }

}

`


-----------Code for Custom resolver ---------------
`

package com;

import javax.xml.transform.URIResolver;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;

public class ResourceResolver implements URIResolver {

    /* (non-Javadoc)
     * @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)
     */
    public Source resolve(String href, String base) throws TransformerException {

        try {

            InputStream is = ClassLoader.getSystemResourceAsStream(href);
            return new StreamSource(is, href);
        } // try
        catch (Exception ex) {
            throw new TransformerException(ex);
        } // catch
    } // resolve

    /**
     * @param href
     * @return
     * @throws TransformerException
     */
    public InputStream resolve(String href) throws TransformerException {
        try {

            InputStream is = ClassLoader.getSystemResourceAsStream(href);
            return is;
        } // try
        catch (Exception ex) {
            throw new TransformerException(ex);
        } // catch
    }
} // ResourceResolver

`

旧时模样 2024-10-11 17:50:56

试试这个

String pathWithinJar = "com/example/xslt/dummy.xslt";
InputStream is = java.lang.ClassLoader.getSystemResourceAsStream(pathWithinJar);

然后你可以使用 IOUtils (apache) 或建议之一 此处 将 InputStream 转换为 String 或仅使用接受输入流的 javax.xml...StreamSource 构造函数。

public static void transform(InputStream xslFileStream, File xmlSource, File xmlResult)
      throws TransformerException, IOException {

    // unknown if the factory is thread safe, always create new instance
    TransformerFactory factory = TransformerFactory.newInstance();
    StreamSource xslStreamSource = new StreamSource(xslFileStream);
    Transformer transformer = factory.newTransformer(xslStreamSource);

    StreamSource sourceDocument = new StreamSource(xmlSource);
    StreamResult resultDocument = new StreamResult(xmlResult);
    transformer.transform(sourceDocument, resultDocument);

    resultDocument.getOutputStream().flush();
    resultDocument.getOutputStream().close();
  }

Try this

String pathWithinJar = "com/example/xslt/dummy.xslt";
InputStream is = java.lang.ClassLoader.getSystemResourceAsStream(pathWithinJar);

Then you can us IOUtils (apache) or one of the suggestions here to convert the InputStream into a String or just use the javax.xml...StreamSource constructor that accepts an input stream.

public static void transform(InputStream xslFileStream, File xmlSource, File xmlResult)
      throws TransformerException, IOException {

    // unknown if the factory is thread safe, always create new instance
    TransformerFactory factory = TransformerFactory.newInstance();
    StreamSource xslStreamSource = new StreamSource(xslFileStream);
    Transformer transformer = factory.newTransformer(xslStreamSource);

    StreamSource sourceDocument = new StreamSource(xmlSource);
    StreamResult resultDocument = new StreamResult(xmlResult);
    transformer.transform(sourceDocument, resultDocument);

    resultDocument.getOutputStream().flush();
    resultDocument.getOutputStream().close();
  }
扮仙女 2024-10-11 17:50:56

InputStream 包含 jar 文件
xslt 文件

你为什么这么说?您是否尝试过将 InputStream 的内容打印为文本?在创建 InputStream in 和使用它之间,您是否用它做了其他事情(...部分)?

如果提供给 getResourceAsStream 的路径指向 XSLT,并且调用后 is 不为 null,则 is应包含表示 XSLT 资源的 InputStream。将整个堆栈跟踪粘贴到此处怎么样?

InputStream contains jar file instead
xslt file

What makes you say that? Have you tried printing out the contents of the InputStream as text? In between creating the InputStream in and using it, are you doing something else with it(the ... part)?

If the path provided to the getResourceAsStream points to an XSLT and if is is not null after the call, is should contain the InputStream representing the XSLT resource. How about pasting the entire stack trace here?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文