[英]Java setURIResolver not being called?
I want to use the setURIResolver callback function provided by javax.xml.transform.Transformer. 我想使用javax.xml.transform.Transformer提供的setURIResolver回调函数。 I have implemented the 'resolve' function but it is not being called. 我已经实现了“解决”功能,但是没有被调用。
public class XSLMagic implements URIResolver {
public void DoXSLTransform(final File xslDoc, final File xmlDoc, final File resultDoc) {
// Create the factory...
TransformerFactory tf = TransformerFactory.newInstance();
// Create the transformer object from
Transformer tr = tf.newTransformer(new StreamSource(xslDoc));
tr.setURIResolver(this); // <--- THIS LINE doesn't seem to work.
tr.transform(new StreamSource(xmlDoc), new StreamResult(resultDoc));
}
@Override
public Source resolve(String href, String base) throws TransformerException {
System.out.print("resolve: " + href + " " + base + "\n");
return null;
}
}
I have tested that it is not being called by the lack of outputted messages, also by setting a debug point on the function and then stepping through. 我已经通过在功能上设置调试点然后逐步执行,测试了缺少输出消息的情况不会调用它。
What am I doing wrong? 我究竟做错了什么?
Worked out the answer as I wrote this... :) 当我写这篇文章时算出答案... :)
Set the setURIResolver on the TransformerFactory, not the Transformer object. 在TransformerFactory而不是Transformer对象上设置setURIResolver。
So the code would be... 因此代码将是...
public class XSLMagic implements URIResolver {
public void DoXSLTransform(final File xslDoc, final File xmlDoc, final File resultDoc) {
// Create the factory...
TransformerFactory tf = TransformerFactory.newInstance();
tf.setURIResolver(this); // WORKS - Set the URIResolver to the factory instead, 'resolve' function now called as expected.
// Create the transformer object from
Transformer tr = tf.newTransformer(new StreamSource(xslDoc));
tr.transform(new StreamSource(xmlDoc), new StreamResult(resultDoc));
}
@Override
public Source resolve(String href, String base) throws TransformerException {
System.out.print("resolve: " + href + " " + base + "\n");
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.