gpt4 book ai didi

XML 创建 - 错误 : overloaded method constructor UnprefixedAttribute with alternatives

转载 作者:数据小太阳 更新时间:2023-10-29 01:40:28 26 4
gpt4 key购买 nike

scala> val count = 7
count: Int = 7

将其放入 XML 属性会产生错误:

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 类:

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

创建了一个 Size 的实例:

scala> val seven = new Size(7, Units.COUNT)
seven: Size = 7.0 count

然后尝试将您的大小放入 XML 属性中,您仍然会收到错误消息:

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 方法。这有效:

scala> val x = <element count={count.toString} size={seven.toString}/>
x: scala.xml.Elem = <element size="7.0 count" count="7"></element>

关于XML 创建 - 错误 : overloaded method constructor UnprefixedAttribute with alternatives,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8041105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com