我在install4j项目中经常使用“在XML文件中替换文本”操作。我的问题是,每次我使用此操作时,它都会在xml文件中添加一个空行。
这是我的示例XPath表达式。
/configuration/appSettings/add[@key='KpsInstallPath']/@value
任何想法我可以针对这种行为做什么? xml文件在安装后很重要,并且很难读取那么多空行(目前,每个XPath表达式有10条,每条20条)
该节点以换行符结尾,该换行符不是语义XML模型的一部分。它只是文本表示的一部分。这导致此症状。除了使用自定义代码或使用“用正则表达式修改文本文件”操作自己修剪这些换行符外,没有其他方法可以解决此问题。能够像“ Pretty-print XML”一样向后执行操作会很不错(imo)。
以下是自定义代码示例:
import java.nio.file.Files;[![enter image description here][1]][1]
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("\r\n\r\n", "").getBytes());
}
可以在这里添加: