2015-11-03 164 views
1

我嘗試將文檔保存到XML中相當輸出。但問題是每個新元素都從前一個元素結束標記行開始。JAVA 8 xml漂亮的輸出不能正常工作

例子:

<?xml version="1.0" encoding="UTF-8"?><report> 
<jiraentry category="SERVICE &amp; ASSET" component="DOCMAN" priority="10" summary="Bug Generated By, UNCHECKED_ACCESS">Tool:UNCHECKED_ACCESS Date:2015-11-12 
</jiraentry> 

你可以看到和文字打印在透水line.Here是下面這在我以前使用的變壓器,以保存XML我的Java代碼。

  //normalize the xml file 
     root.getDocumentElement().normalize(); 
     //remove standalone no 
     root.setXmlStandalone(true); 

     // write the content into xml file 
     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     transformerFactory.setAttribute("indent-number", new Integer(0));//add intend to the new line 

     Transformer transformer = transformerFactory.newTransformer(); 
     transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 

     DOMSource source = new DOMSource(root); 
     StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(this.outpath), "UTF-8"));//save the file 
     transformer.transform(source, result); 

任何人都可以建議我讓這個更漂亮和清晰。

+0

貴原始XML包含換行符? – ThomasRS

+0

他們沒有東西會調用原始的xml,程序通過轉換根來創建xml。 –

回答

0

您需要設置縮進量爲變壓器作爲顯示在下面的代碼片段:

t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

有益的參考:Similar Question

+0

沒有幸運仍然是一樣的 –