Java:将StreamResult转换为DOM
我正在使用saxon和xslt执行从一种xml格式到另一种xml格式的xslt转换,然后将结果xml转换为Java DOM以进一步使用它:Java:将StreamResult转换为DOM,java,xml,xslt,dom,xml-parsing,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 tr
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
文件。有可能避免它的产生吗 DocumentResult=新的DocumentResult()
transform(新StreamSource(新文件(inputXML)),结果)
Document transformedDoc=result.getDocument() 答案由@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();
使用。阅读文档。请做点调查,我不知道。我们整天都在寻找解决办法。也许我太蠢了,不适合编程:(安德烈亚斯,这可能需要一个答案