Java: convert StreamResult to DOM

五迷三道 提交于 2019-12-23 01:36:15

问题


I am performing a xslt transformation from one xml format to another with saxon and xslt, after what transforming the result xml into Java DOM to work with it further:

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);

What I don't like in this situation, is creating intermediate outputXML file. Is it possible to avoid its creating?


回答1:


The answer was provided by @Andreas. If someone is interested, the result code snippet in my case looked like this:

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();



回答2:


DocumentResult result = new DocumentResult();

transformer.transform( new StreamSource(new File(inputXML)), result );

Document transformedDoc = result.getDocument();



来源:https://stackoverflow.com/questions/42587753/java-convert-streamresult-to-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!