2015-11-03 165 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

没有幸运仍然是一样的 –