如何强制java xml dom在?

时间:2023-01-06 08:08:35

I use this code to enable newlines:

我使用此代码启用换行符:

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

But I get following output:

但我得到以下输出:

<?xml version="1.0" encoding="UTF-8"?><root>
  <child>aaaa</child>
</root>

I'd like to have newline before root element. What should I do?

我想在root元素之前有换行符。我该怎么办?

4 个解决方案

#1


3  

Try this

尝试这个

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

#2


1  

Another solution is : transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "10");

另一个解决方案是:transformer.setOutputProperty(OutputKeys.INDENT,“yes”); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, “是”); transformer.setOutputProperty(“{http://xml.apache.org/xslt}indent-amount”,“10”);

#3


0  

You could probably add the newline yourself by wrapping a Writer in a FilterWriter (or OutputStream in a FilterOutputStream) coded to recognize the end of that tag and add a newline, on the way out.

你可以自己添加新行,方法是将一个Writer包装在FilterWriter(或FilterOutputStream中的OutputStream)中,编码以识别该标记的结尾并在出路时添加换行符。

But that's a bit silly. As Stephen C notes in his comment, you really probably shouldn't care about this enough to bother.

但那有点傻。正如斯蒂芬C在他的评论中指出的那样,你真的可能不应该关心这个问题。

#4


-1  

you can insert the newline manually. Assign the string to a temporary variable (let say its String temp). Then do somehting like this

您可以手动插入换行符。将字符串分配给临时变量(比如它的String temp)。然后像这样做

String filteredStr = new StringBuilder(temp).insert(temp.indexOf('>') + 1, "\n")
                    .toString();

#1


3  

Try this

尝试这个

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

#2


1  

Another solution is : transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "10");

另一个解决方案是:transformer.setOutputProperty(OutputKeys.INDENT,“yes”); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, “是”); transformer.setOutputProperty(“{http://xml.apache.org/xslt}indent-amount”,“10”);

#3


0  

You could probably add the newline yourself by wrapping a Writer in a FilterWriter (or OutputStream in a FilterOutputStream) coded to recognize the end of that tag and add a newline, on the way out.

你可以自己添加新行,方法是将一个Writer包装在FilterWriter(或FilterOutputStream中的OutputStream)中,编码以识别该标记的结尾并在出路时添加换行符。

But that's a bit silly. As Stephen C notes in his comment, you really probably shouldn't care about this enough to bother.

但那有点傻。正如斯蒂芬C在他的评论中指出的那样,你真的可能不应该关心这个问题。

#4


-1  

you can insert the newline manually. Assign the string to a temporary variable (let say its String temp). Then do somehting like this

您可以手动插入换行符。将字符串分配给临时变量(比如它的String temp)。然后像这样做

String filteredStr = new StringBuilder(temp).insert(temp.indexOf('>') + 1, "\n")
                    .toString();