首页 > 解决方案 > 为什么 install4j 在我的 xml 文件中添加空行

问题描述

我在我的 install4j 项目中多次使用“替换 XML 文件中的文本”操作。我的问题是,每次我使用此操作时,它都会在 xml 文件中添加一个空行。

这是我的示例 XPath 表达式。

/configuration/appSettings/add[@key='KpsInstallPath']/@value

知道我能对这种行为做些什么吗?xml 文件在安装后很重要,而且有那么多空行很难阅读(目前每个 XPath 表达式 10 个,20 个条目)

标签: xpathinstall4j

解决方案


该节点以不属于语义 XML 模型的换行符结束。它只是文本表示的一部分。这导致了这种症状。除了使用自定义代码或“使用正则表达式修改文本文件”操作自己修剪这些换行符之外,没有其他办法。以后有一个像“Pretty-print XML”这样的动作会很好(imo)。

自定义代码示例如下:

import java.nio.file.Files;

public static void prettyPrintXML(String filePath) throws Exception {
    final StringWriter writer = new StringWriter();
    File out = new File(filePath);
    try (final FileReader reader = new FileReader(out)) {
        org.codehaus.plexus.util.xml.XmlUtil.prettyFormat(new FileReader(new File(filePath)), writer);    
    }
    Files.write(out.toPath(), writer.getBuffer().toString().replace(System.lineSeparator()+System.lineSeparator(), "").getBytes());

}

可以在这里添加:

在此处使用此 JAR 的代码:https ://mvnrepository.com/artifact/org.codehaus.plexus/plexus-utils/3.3.0 。

这很好地清理了我的 XML。


推荐阅读