w3c.dom.Document
可以用作Node
,但是当我尝试这个
public static Object tranform(Source source) {
Object result = null;
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer newTransformer = tf.newTransformer();
newTransformer.transform(source, (Result) result);
} catch (TransformerException ex) {
Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);
}
return result;
}
在另一个文件上
Document doc = (Document) XMLUtils.tranform(source);
// pass this doc to a function that that a Node typed parameter
if (node == null) {
return;
} else {
...
}
node
始终为 null,并且它也没有子节点。
我尝试将每种类型的源转换为每种类型的结果,编码人员会将返回的结果转换为他们需要的正确类型。那么可以请你吗
1)解释一下我的错误?
2)给我一个提示如何实现这个目的?
谢谢您并致以诚挚的问候
最佳答案
我认为您需要创建一个实现 Result
的类,例如 DOMResult
:
Result result = new DOMResult();
newTransformer.transform(source, result);
return result.getNode();
关于java - 相同的 org.w3c.dom.Document 变量的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17641399/