Scala - 修改 xml 中的嵌套元素 [英] Scala - modifying nested elements in xml

查看:39
本文介绍了Scala - 修改 xml 中的嵌套元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Scala,我希望更新一些 xml 中的嵌套节点.我有一些工作,但我想知道它是否是最优雅的方式.

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.

我有一些 xml:

val InputXml : Node =
<root>
    <subnode>
        <version>1</version>
    </subnode>
    <contents>
        <version>1</version>
    </contents>
</root>

我想更新 subnode 中的 version 节点,而不是 contents 中的那个.

And i want to update the version node in subnode, but not the one in contents.

这是我的功能:

def updateVersion( node : Node ) : Node = 
 {
   def updateElements( seq : Seq[Node]) : Seq[Node] = 
   {
        var subElements = for( subNode <- seq ) yield
        {
            updateVersion( subNode )
        }   
        subElements
   }

   node match
   {
     case <root>{ ch @ _* }</root> =>
     {
        <root>{ updateElements( ch ) }</root>
     }
     case <subnode>{ ch @ _* }</subnode> =>
     {
         <subnode>{ updateElements( ch ) }</subnode> 
     }
     case <version>{ contents }</version> =>
     {
        <version>2</version>
     }
     case other @ _ => 
     {
         other
     }
   }
 }

有没有更简洁的方法来编写这个函数?

Is there a more succint way of writing this function?

推荐答案

我觉得原来的逻辑很好.这是相同的代码(我敢说吗?)更具 Scala 风格:

I think the original logic is good. This is the same code with (shall I dare to say?) a more Scala-ish flavor:

def updateVersion( node : Node ) : Node = {
   def updateElements( seq : Seq[Node]) : Seq[Node] = 
     for( subNode <- seq ) yield updateVersion( subNode )  

   node match {
     case <root>{ ch @ _* }</root> => <root>{ updateElements( ch ) }</root>
     case <subnode>{ ch @ _* }</subnode> => <subnode>{ updateElements( ch ) }</subnode>
     case <version>{ contents }</version> => <version>2</version>
     case other @ _ => other
   }
 }

看起来更紧凑(但实际上是一样的:))

It looks more compact (but is actually the same :) )

  1. 我摆脱了所有不必要的括号
  2. 如果需要括号,它从同一行
  3. updateElements 只是定义了一个 var并返回它,所以我摆脱了它并直接返回结果

如果你愿意,你也可以去掉 updateElements.您希望将 updateVersion 应用于序列的所有元素.那是 地图方法.有了这个,你可以重写这一行

if you want, you can get rid of the updateElements too. You want to apply the updateVersion to all the elements of the sequence. That's the map method. With that, you can rewrite the line

case <subnode>{ ch @ _* }</subnode> => <subnode>{ updateElements( ch ) }</subnode>

case <subnode>{ ch @ _* }</subnode> => <subnode>{ ch.map(updateVersion (_)) }</subnode>

由于更新版本只需要 1 个参数,我 99% 确定您可以省略它并写入:

As update version takes only 1 parameter I'm 99% sure you can omit it and write:

case <subnode>{ ch @ _* }</subnode> => <subnode>{ ch.map(updateVersion) }</subnode>

并以:

def updateVersion( node : Node ) : Node = node match {
         case <root>{ ch @ _* }</root> => <root>{ ch.map(updateVersion )}</root>
         case <subnode>{ ch @ _* }</subnode> => <subnode>{ ch.map(updateVersion ) }</subnode>
         case <version>{ contents }</version> => <version>2</version>
         case other @ _ => other
       }

你怎么看?

这篇关于Scala - 修改 xml 中的嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆