【问题标题】:How to remove empty lines from an updated XML?如何从更新的 XML 中删除空行?
【发布时间】:2017-02-25 11:55:32
【问题描述】:

所以我正在做的是,我正在从 XML 中查找并删除一些标签。删除后,这些标签会被空格替换,这实际上不会影响 XML,但是当我删除大量标签时会出现问题。

有什么可行的方法来实现这一点吗?

提前致谢。

Here is the link of the answer I found here but it's not working

【问题讨论】:

  • 欢迎来到 Stack Overflow!请查看我们的SO Question Checklist 以帮助您提出一个好问题,从而得到一个好的答案。

标签: java xml dom


【解决方案1】:

这应该可行:

public static void main(String[] args) {

    Scanner file;
    PrintWriter writer;

    try {

        file = new Scanner(new File("src/myXml.xml"));
        writer = new PrintWriter("src/myXml2.xml");

        while (file.hasNext()) {
            String line = file.nextLine();
            if (!line.isEmpty()) {
                writer.write(line);
                writer.write("\n");
            }
        }

        file.close();
        writer.close();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }
}

【讨论】:

  • 感谢您的回复。
  • 但它不适用于 XML。我想,XML 遵循了其他的东西。
猜你喜欢
  • 1970-01-01
  • 2012-09-22
  • 1970-01-01
  • 2018-07-18
  • 2012-10-20
  • 2011-10-24
  • 2016-01-20
  • 1970-01-01
  • 2021-09-13
相关资源
最近更新 更多