问题
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