Java:将 StreamResult 转换为 DOM

标签 java xml xslt dom xml-parsing

我正在使用 saxon 和 xslt 执行从一种 xml 格式到另一种 xml 格式的 xslt 转换,然后将结果 xml 转换为 Java DOM 以进一步使用它:

System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(new StreamSource(new File("xslt.xsl")));
transformer.transform(new StreamSource(new File(inputXML)),
                                  new StreamResult(new File (outputXML)));

DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = documentBuilder.parse(outputXML);

在这种情况下,我不喜欢创建中间 outputXML 文件。是否有可能避免它的产生?

最佳答案

答案由@Andreas提供。如果有人感兴趣,我的例子中的结果代码片段如下所示:

System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory tFactory = TransformerFactory.newInstance();

DOMResult xmlResult = new DOMResult();
Transformer transformer = tFactory.newTransformer(new StreamSource(new File("xslt.xsl")));
        transformer.transform(new StreamSource(new File(outputXML)),
                              xmlResult);

Document document = (Document) xmlResult.getNode();

关于Java:将 StreamResult 转换为 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42587753/

相关文章:

单击时的 JavaFX 形状

xml - 如何使用 weserver 配置设置在 IIS 7 中禁用单个文件的缓存

xml - 如何为一本书构建和标记 XML 中的对话?

java - 将所有请求参数映射到类对象中

java - 用 Java 存储可读可写的资源

java - 部署时使用 Guice InstantiationError 进行 RESTEasy(App Engine 本地)

xml - XSLT - 从模板中删除空格

c# - 用 XElement 替换 XElement 内容

xml - For 循环与应用模板

xslt - exsl :document - trying to generate output file