输出JSoup,不添加空格和元素周围的换行符 [英] Output JSoup without added spaces and line breaks around the elements
本文介绍了输出JSoup,不添加空格和元素周围的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JSoup解析并输出一个xml文件(当然还要修改它们之间的元素)。
I am parsing and outputting an xml file using JSoup (and modifying the elements in between of course).
输出文件有一些额外的空格和换行符。我想知道我是否可以用原始格式打印。
The output file has some extra spaces and line breaks. I was wondering if I can print this in the original format.
原文:
<attributes>
<divisions>4</divisions>
<key>
<fifths>0</fifths>
<mode>major</mode>
</key>
...
新:
<attributes>
<divisions>
4
</divisions>
<key>
<fifths>
0
</fifths>
<mode>
major
</mode>
</key>
...
关于如何从元素中删除空格/进入的任何想法?
Any idea on how to remove the spaces/enters from the elements?
我目前读入并打印文档如下:
I currently read in and print the document like this:
doc = Jsoup.parse(is, "UTF-8", "", Parser.xmlParser());
BufferedWriter htmlWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("output.xml"), "UTF-8"));
htmlWriter.write(doc.toString());
推荐答案
在Aleksandr M 我通过以下方式解决了这个问题:
With some help from Aleksandr M I solved it in the following way:
doc.outputSettings().indentAmount(0).prettyPrint(false);
稍微不那么好,但这似乎也可以解决问题:
A little less nice, but this also seemed to do the trick:
htmlWriter.write(doc.toString().replaceAll(">\\s+",">").replaceAll("\\s+<","<"));
这篇关于输出JSoup,不添加空格和元素周围的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文