如何将代码从HasKeyValue迁移到UnprefixedAttribute?

时间:2011-07-31 11:37:53

标签: xml scala migration deprecated

考虑以下代码:

  import scala.xml.{Node,HasKeyValue}

  def domatch(x:Node): Node = {
    val hasBar = new HasKeyValue("bar")

    x match {
      case Node("foo", hasBar(z), _*) => z
      case _ => null
    }
  }

编译时,我收到以下警告:

error: class HasKeyValue in package xml is deprecated: 
       Use UnprefixedAttribute's extractor

代码应该如何?

1 个答案:

答案 0 :(得分:2)

scala> import xml._
import xml._

scala> val hasBar = new HasKeyValue("bar")
<console>:10: warning: class HasKeyValue in package xml is deprecated: Use UnprefixedAttribute's extractor
       val hasBar = new HasKeyValue("bar")
           ^
<console>:10: warning: class HasKeyValue in package xml is deprecated: Use UnprefixedAttribute's extractor
       val hasBar = new HasKeyValue("bar")
                        ^
hasBar: scala.xml.HasKeyValue = scala.xml.HasKeyValue@62a28815

scala> <e bar="bar"/> match { case Node(_, hasBar(es), _*) => es }
res0: scala.xml.Node = bar

scala> <e bar="blerg"/> match { case Node(_, UnprefixedAttribute(_, es, _), _*) => es }
res1: Seq[scala.xml.Node] = blerg