【发布时间】:2019-01-23 10:18:38
【问题描述】:
我正在编写一个工具来用 scala 更新一些 xml 文件(在这种情况下是 pom.xml),因为它在 java 中所花费的工作量明显高于(理论上)它在 scala 中的工作量。我可以很好地解析xml文件,但是我需要替换现有xml中的节点并重写结果。例如:
<dependency>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
所以我想找到所有这样的节点并将它们替换为:
<dependency>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>1.0</version> <!-- notice the lack of -SNAPSHOT here -->
</dependency>
所以,我可以简单地获取所有版本节点,但是如何将它们替换为我想要的节点呢?
// document is already defined as the head of the xml file
nodes = for (node <- document \\ "version"; if (node.text.contains("SNAPSHOT"))) yeild node
然后我想做类似的事情:
for (node <- nodes) {
node.text = node.text.split("-")(0)
}
这不起作用,因为节点是不可变的。我查看了节点的复制方法,但它不包含 text 作为参数。
【问题讨论】: