2017-03-03 19 views
0

我執行從一個XML格式的XSLT轉換到另一個撒克遜和XSLT,以後有什麼轉變的結果XML轉換成Java的DOM與它進一步的工作:的Java:轉換StreamResult到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。是否有可能避免其創建?

+2

使用['DOMResult'](https://docs.oracle.com/javase/8/docs/api/javax/xml /transform/dom/DOMResult.html)。閱讀文檔。請做一點研究*。 – Andreas

+0

不知道。一直在尋求解決方案。也許我太愚蠢編程:( – Vitaliy

+0

安德烈亞斯,這可能應該被轉移到答案 – MeBigFatGuy

回答

0

DocumentResult result = new DocumentResult(); (新的StreamSource(new File(inputXML)),result);

Document transformedDoc = result.getDocument();

0

答案由@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();