2011-03-23 156 views
0

當我有一個從XML節點提取作用對象的方法:斯卡拉+電梯:Ambigous隱式轉換解析XML

private def appendActionsFromXml(device: Device, xml: Node) = { 
    xml \ "actions" \ "action" map { 
     x => { 
      val key = x \ "@key" text 
      val value = x \ "@value" text 
      device.createAction(key, value) 
     } 
    } 
} 

但是,因爲我已經導入進口net.liftweb.json.JsonDSL。 _在同一個班級,我得到一個ambigouity當我從X提取「@key」 -attribute:

[INFO] Note that implicit conversions are not applicable because they are ambiguous 
[INFO] both method string2jvalue in trait Implicits of type (x: String)net.liftweb.json.JsonAST.JString 
[INFO] and method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps 
[INFO] are possible conversion functions from String to ?{val apply: ?} 
[INFO] val value = x \ "@value" text 

如何在這個perticular方法解決此ambigouity?

回答

0

試試這個:如果可能的話

val key: String = x \ "@key" text 
val value: String = x \ "@value" text 
0

移動你的JsonDSL進口(或反向的XML-進口)到一個較小的範圍內。

class A { 
    def doXmlStuff = { ... } 
    def doJsonStuff = { 
    import net.liftweb.json.JsonDSL._ 
    ... 
    } 
} 
0

通常解決此類問題的方法是減少導入的範圍。在這種情況下,您可能不需要在範圍內包含net.liftweb.json.JsonDSL._範圍內的appendActionsFromXml。很難說如果沒有看到更多的上下文,這將工作。