Java 在XML中仅在第一行预先添加空格
我在通过Java代码生成XML时遇到了一个问题。我将xml直接打印到网页上以显示统计数据。生成的XML的第一行总是在开头有一些空格,这使得XML看起来很奇怪。我如何删除相同的。我尝试删除xml行,但不管是哪一行,它都有预空格 请让我现在如何删除它Java 在XML中仅在第一行预先添加空格,java,xml,dom,Java,Xml,Dom,我在通过Java代码生成XML时遇到了一个问题。我将xml直接打印到网页上以显示统计数据。生成的XML的第一行总是在开头有一些空格,这使得XML看起来很奇怪。我如何删除相同的。我尝试删除xml行,但不管是哪一行,它都有预空格 请让我现在如何删除它 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <headNode> <statsHead1> <totalCount>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<headNode>
<statsHead1>
<totalCount>0.05</totalCount>
</statsHead1>
<statsHead2>
<statsSubHead1>
<count1>0</count1>
<count2>0.0</count2>
</statsSubHead1>
<statsSubHead2>
<count1>0</count1>
<count2>0.0</count2>
</statsSubHead2>
<totalcount1>0</totalcount1>
<totalcount2>0.0</totalcount2>
</statsHead2>
</headNode>
您的生成器是什么?刚刚添加了代码,您可以在这里看到一种实现方式:这是首先提到的。
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(doc);
StringWriter outWriter = new StringWriter();
Result result = new StreamResult(outWriter);
transformer.transform(source, result);
return outWriter.toString();