substituting xml values programmatically with scala

后端 未结 3 1684
有刺的猬
有刺的猬 2021-01-03 04:42

I\'m writing a tool to update some xml files (pom.xml in this case) with scala because the effort it would take in java is significantly higher than (in theory) it is with s

3条回答
  •  情歌与酒
    2021-01-03 05:25

    You really should take a look at other questions on Stack Overflow about modifying XML. Look at the "Related" links to the right.

    Here:

    scala> 
         |     foo
         |     bar
         |     1.0-SNAPSHOT
         | 
    res0: scala.xml.Elem =
    
        foo
        bar
        1.0-SNAPSHOT
    
    
    scala> new scala.xml.transform.RewriteRule {
         |   override def transform(n: Node): Seq[Node] = n match {
         |     case {v} if v.text contains "SNAPSHOT" => {v.text.split("-")(0)}
         |     case elem: Elem => elem copy (child = elem.child flatMap (this transform))
         |     case other => other
         |   }
         | } transform res0
    res9: Seq[scala.xml.Node] =
    
        foo
        bar
        1.0
    
    

提交回复
热议问题