2011-03-23 178 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。很难说如果没有看到更多的上下文,这将工作。