Scala - modifying nested elements in xml

后端 未结 7 653
太阳男子
太阳男子 2020-12-02 11:11

I\'m learning scala, and I\'m looking to update a nested node in some xml. I\'ve got something working but i\'m wondering if its the most elegant way.

I have some xm

7条回答
  •  清歌不尽
    2020-12-02 12:06

    Scales Xml provides tools for "in place" edits. Of course its all immutable but here's the solution in Scales:

    val subnodes = top(xml).\*("subnode"l).\*("version"l)
    val folded = foldPositions( subnodes )( p => 
      Replace( p.tree ~> "2"))
    

    The XPath like syntax is a Scales signature feature, the l after the string specifies it should have no namespace (local name only).

    foldPositions iterates over the resulting elements and transforms them, joining the results back together.

提交回复
热议问题