我正在使用 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/