XML 创建 - 错误:重载方法构造函数 UnprefixedAttribute 和替代项 [英] XML Creation - error: overloaded method constructor UnprefixedAttribute with alternatives
本文介绍了XML 创建 - 错误:重载方法构造函数 UnprefixedAttribute 和替代项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
scala> val count = 7
count: Int = 7
将其放入 XML 属性会产生错误:
putting that into an XML attribute gives an error:
scala> val x = <element count={count}/>
<console>:8: error: overloaded method constructor UnprefixedAttribute with alternatives:
(key: String,value: Option[Seq[scala.xml.Node]],next: scala.xml.MetaData)scala.xml.UnprefixedAttribute <and>
(key: String,value: String,next: scala.xml.MetaData)scala.xml.UnprefixedAttribute <and>
(key: String,value: Seq[scala.xml.Node],next1: scala.xml.MetaData)scala.xml.UnprefixedAttribute
cannot be applied to (java.lang.String, Int, scala.xml.MetaData)
val x = <element count={count}/>
推荐答案
XML 属性的输入必须是字符串.整数和对象不会使用它们的 toString 方法自动转换为字符串.例如,如果您使用 Units 枚举定义了 Size 类:
Inputs to XML attributes must be Strings. Integers and objects will not automatically be converted to Strings using their toString method. For example if you defined a Size class using a Units enum:
scala> object Units extends Enumeration {
| type Units = Value
| val COUNT = Value("count")
| val LB = Value("pounds")
| val OZ = Value("ounces")
| val GRAM = Value("grams")
| val KG = Value("kilograms")
| val GAL = Value("gallons")
| val QT = Value("quarts")
| val PINT = Value("pints")
| val FL_OZ = Value("fluid ounces")
| val L = Value("liters")
| }
defined module Units
scala> class Size(val value: Double, val unit: Units.Units) {
| override def toString = value + " " + unit.toString
| }
defined class Size
创建了一个大小的实例:
created an instance of Size:
scala> val seven = new Size(7, Units.COUNT)
seven: Size = 7.0 count
然后尝试将您的大小放入 XML 属性中,您仍然会收到错误:
then tried to put your size into an XML attribute, you would still get an error:
scala> val x = <element size={seven}/>
<console>:10: error: overloaded method constructor UnprefixedAttribute with alternatives:
(key: String,value: Option[Seq[scala.xml.Node]],next: scala.xml.MetaData)scala.xml.UnprefixedAttribute <and>
(key: String,value: String,next: scala.xml.MetaData)scala.xml.UnprefixedAttribute <and>
(key: String,value: Seq[scala.xml.Node],next1: scala.xml.MetaData)scala.xml.UnprefixedAttribute
cannot be applied to (java.lang.String, Size, scala.xml.MetaData)
val x = <element size={seven}/>
^
您必须显式调用 toString 方法.这有效:
You must explicitly call the toString method. This works:
scala> val x = <element count={count.toString} size={seven.toString}/>
x: scala.xml.Elem = <element size="7.0 count" count="7"></element>
这篇关于XML 创建 - 错误:重载方法构造函数 UnprefixedAttribute 和替代项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文