相关疑难解决方法(0)

如何更改Scala XML Element的属性

我有一个XML文件,我想用脚本映射in的一些属性.例如:

<a>
  <b attr1 = "100" attr2 = "50"/>
</a>
Run Code Online (Sandbox Code Playgroud)

可能有两个缩放的属性:

<a>
  <b attr1 = "200" attr2 = "100"/>
</a>
Run Code Online (Sandbox Code Playgroud)

此页面提供了添加属性的建议,但没有详细说明使用函数映射当前属性的方法(这种方式会非常困难):http: //www.scalaclass.com/book/export/html/1

我想出的是手动创建XML(非scala)链表...类似于:

// a typical match case for running thru XML elements:
case  Elem(prefix, e, attributes, scope, children @ _*) => {
 var newAttribs = attributes
 for(attr <- newAttribs)  attr.key match {
  case "attr1" => newAttribs = attribs.append(new UnprefixedAttribute("attr1", (attr.value.head.text.toFloat * 2.0f).toString, attr.next))
  case "attr2" => newAttribs = attribs.append(new UnprefixedAttribute("attr2", (attr.value.head.text.toFloat * 2.0f).toString, attr.next))
  case …
Run Code Online (Sandbox Code Playgroud)

xml scala

21
推荐指数
3
解决办法
9617
查看次数

你能用antixml来创建xml文件吗?

有一些使用Anti-Xml从XML文档中提取信息的示例,但我没有找到使用Anti-Xml来创建XML文档的示例.Anti-Xml是否支持创建文档,或者我应该使用另一个库(哪一个?).有没有人有一个用Anti-Xml创建XML文档的例子?

xml scala anti-xml

8
推荐指数
1
解决办法
681
查看次数

用scala以编程方式替换xml值

我正在编写一个工具来使用scala更新一些xml文件(本例中为pom.xml),因为它在java中的工作量明显高于(理论上)scala.我可以很好地解析xml文件,但我需要替换现有xml中的节点并重写结果.例如:

<dependency>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

所以我想找到这样的所有节点并将其替换为:

<dependency>
    <groupId>foo</groupId>
    <artifactId>bar</artifactId>
    <version>1.0</version> <!-- notice the lack of -SNAPSHOT here -->
</dependency>
Run Code Online (Sandbox Code Playgroud)

所以,我可以简单地获得所有版本节点,但是如何用我想要的节点替换它们?

// document is already defined as the head of the xml file
nodes = for (node <- document \\ "version"; if (node.text.contains("SNAPSHOT"))) yeild node
Run Code Online (Sandbox Code Playgroud)

那我想做点什么:

for (node <- nodes) {
    node.text = node.text.split("-")(0)
}
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为节点是不可变的.我查看了Node的复制方法,但它不包含text在参数中.

xml scala

4
推荐指数
1
解决办法
3428
查看次数

标签 统计

scala ×3

xml ×3

anti-xml ×1