java - 输出 JSoup,元素周围不添加空格和换行符

标签 java xml jsoup musicxml

我正在使用 JSoup 解析和输出一个 xml 文件(当然还要修改其间的元素)。

输出文件有一些额外的空格和换行符。我想知道我是否可以以原始格式打印它。

原文:

  <attributes>
        <divisions>4</divisions>
        <key>
          <fifths>0</fifths>
          <mode>major</mode>
          </key>
...

新:

<attributes> 
    <divisions>
     4
    </divisions> 
    <key> 
     <fifths>
      0
     </fifths> 
     <mode>
      major
     </mode> 
    </key> 
...

关于如何从元素中删除空格/输入有什么想法吗?

我目前像这样读入并打印文档:

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 的帮助下我通过以下方式解决了它:

doc.outputSettings().indentAmount(0).prettyPrint(false);

不太好,但这似乎也能解决问题:

htmlWriter.write(doc.toString().replaceAll(">\\s+",">").replaceAll("\\s+<","<"));

关于java - 输出 JSoup,元素周围不添加空格和换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28877155/

相关文章:

java - Dijkstra算法Java : Last edge always gets added instead of shortest

java - Spring Data MongoDb 是否支持 $filter 数组聚合运算符?

java - BFS五字母单词链

python - 通过其相邻值推导 html 元素

c++ - arg 方向的 "in"和 "out"是什么意思?

java - 如何在 JSOUP 中获取显示/视觉图像大小(而不是实际图像大小)?

javascript - 使用jsoup解析 "dynamic"页面

java - 插件的 Flutter 构建失败,找不到类文件

android - 继续 ProgressDialog 直到 onPostExecute 完成

php - 使用 XML 和 XPath 的 Symfony DomCrawler