xpath - 为什么 install4j 在我的 xml 文件中添加空行
问题描述
我在我的 install4j 项目中多次使用“替换 XML 文件中的文本”操作。我的问题是,每次我使用此操作时,它都会在 xml 文件中添加一个空行。
这是我的示例 XPath 表达式。
/configuration/appSettings/add[@key='KpsInstallPath']/@value
知道我能对这种行为做些什么吗?xml 文件在安装后很重要,而且有那么多空行很难阅读(目前每个 XPath 表达式 10 个,20 个条目)
解决方案
该节点以不属于语义 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。
推荐阅读
- database - 以下分解是否无损且依赖关系保持不变?
- reactjs - 如何在 REACT useEffect 函数中从 webservice 获取数据并将其发送到 props 对象?
- javascript - JS 中的 Importing(require) 模块是否也执行整个代码?
- c - 我需要一个替换 func1,2,3,4,5 的函数
- html - notepad++ 有没有办法连接一个使用外部 style.css 文件的方法。?
- arrays - 在Java中向某个数组索引添加数量
- c++ - 使用 BOOST_STRONG_TYPEDEF 区分 arg 类型但导致 seg 错误
- amazon-web-services - aws bottlerocketOS 容器运行时
- sql - 为什么 Column Viti 为空?
- batch-file - 如何在当前 PC 时间上增加几秒?