从 JAR 文件加载 XSLT 文件会加载 JAR 文件本身而不是 XSLT
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建自定义解析器以从类路径解析
将其设置为 Transfromer
为了测试我在 Eclipse 项目的类路径中设置了一个 jar 文件,
所有代码都在下面
----- 运行示例-------------
`
<代码>
-----------示例转换示例 ----------------
`
-----------自定义解析器代码 ---------------
`
`
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 -------------
`
-----------Sample transfomring example ----------------
`
-----------Code for Custom resolver ---------------
`
`
试试这个
然后你可以使用 IOUtils (apache) 或建议之一 此处 将 InputStream 转换为 String 或仅使用接受输入流的 javax.xml...StreamSource 构造函数。
Try this
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.
你为什么这么说?您是否尝试过将
InputStream
的内容打印为文本?在创建 InputStreamin
和使用它之间,您是否用它做了其他事情(...部分)?如果提供给
getResourceAsStream
的路径指向 XSLT,并且调用后is
不为null
,则is
应包含表示 XSLT 资源的InputStream
。将整个堆栈跟踪粘贴到此处怎么样?What makes you say that? Have you tried printing out the contents of the
InputStream
as text? In between creating the InputStreamin
and using it, are you doing something else with it(the ... part)?If the path provided to the
getResourceAsStream
points to an XSLT and ifis
is notnull
after the call,is
should contain theInputStream
representing the XSLT resource. How about pasting the entire stack trace here?