使用 xstream 在 Scala 中将 xml 转换为对象或映射时出错
Error while Convert xml to Object or Map in Scala using xstream
我正在尝试使用 xstream 将 xml 隐藏到对象:-
def convert(xml: String) = {
val xstream = new XStream(new DomDriver)
val convertedMap:testing = xstream.fromXML(xml).asInstanceOf[testing];
convertedMap
}
val justtest:String = "<testing><note>5</note></testing>"
convert(justtest)
测试class定义如下:-
class testing {
private var note = None;
}
我收到以下错误:-
java.lang.InstantiationError: testing
at sun.reflect.GeneratedSerializationConstructorAccessor28.newInstance(sum.sc)
at java.lang.reflect.Constructor.newInstance(sum.sc:419)
at com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider.newInstance(sum.sc:71)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(sum.sc:424)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(sum.sc:229)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(sum.sc:68)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(sum.sc:61)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(sum.sc:62)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(sum.sc:46)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(sum.sc:130)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(sum.sc:28)
at com.thoughtworks.xstream.XStream.unmarshal(sum.sc:1054)
at com.thoughtworks.xstream.XStream.unmarshal(sum.sc:1038)
at com.thoughtworks.xstream.XStream.fromXML(sum.sc:909)
at com.thoughtworks.xstream.XStream.fromXML(sum.sc:900)
at #worksheet#.convert(sum.sc:26)
at #worksheet#.#worksheet#(sum.sc:30)
请帮忙解决以上问题。
还有没有办法直接将 xml 转换成 scala 中的地图?
我没问题运行你的代码:
import com.thoughtworks.xstream.XStream
import com.thoughtworks.xstream.io.xml.DomDriver
object Main {
def main(args: Array[String]): Unit = {
val justtest:String = "<Testing><note>5</note></Testing>"
convert(justtest)
}
def convert(xml: String) = {
val xstream = new XStream(new DomDriver)
val convertedMap: Testing = xstream.fromXML(xml).asInstanceOf[Testing]
convertedMap
}
}
class Testing {
private var note = None
}
也许你的testing
class不在默认包里,所以是打包问题?
无论如何,您希望 None
类型的变量如何存储您的数据?也许您应该将其设为 String 或 Int。我不熟悉 Xstream,但如果你想让库直接映射 xml 到 Option,我认为你需要编写一个自定义转换器。这也适用于转换 to/from 地图。
我正在尝试使用 xstream 将 xml 隐藏到对象:-
def convert(xml: String) = {
val xstream = new XStream(new DomDriver)
val convertedMap:testing = xstream.fromXML(xml).asInstanceOf[testing];
convertedMap
}
val justtest:String = "<testing><note>5</note></testing>"
convert(justtest)
测试class定义如下:-
class testing {
private var note = None;
}
我收到以下错误:-
java.lang.InstantiationError: testing
at sun.reflect.GeneratedSerializationConstructorAccessor28.newInstance(sum.sc)
at java.lang.reflect.Constructor.newInstance(sum.sc:419)
at com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider.newInstance(sum.sc:71)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(sum.sc:424)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(sum.sc:229)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(sum.sc:68)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(sum.sc:61)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(sum.sc:62)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(sum.sc:46)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(sum.sc:130)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(sum.sc:28)
at com.thoughtworks.xstream.XStream.unmarshal(sum.sc:1054)
at com.thoughtworks.xstream.XStream.unmarshal(sum.sc:1038)
at com.thoughtworks.xstream.XStream.fromXML(sum.sc:909)
at com.thoughtworks.xstream.XStream.fromXML(sum.sc:900)
at #worksheet#.convert(sum.sc:26)
at #worksheet#.#worksheet#(sum.sc:30)
请帮忙解决以上问题。
还有没有办法直接将 xml 转换成 scala 中的地图?
我没问题运行你的代码:
import com.thoughtworks.xstream.XStream
import com.thoughtworks.xstream.io.xml.DomDriver
object Main {
def main(args: Array[String]): Unit = {
val justtest:String = "<Testing><note>5</note></Testing>"
convert(justtest)
}
def convert(xml: String) = {
val xstream = new XStream(new DomDriver)
val convertedMap: Testing = xstream.fromXML(xml).asInstanceOf[Testing]
convertedMap
}
}
class Testing {
private var note = None
}
也许你的testing
class不在默认包里,所以是打包问题?
无论如何,您希望 None
类型的变量如何存储您的数据?也许您应该将其设为 String 或 Int。我不熟悉 Xstream,但如果你想让库直接映射 xml 到 Option,我认为你需要编写一个自定义转换器。这也适用于转换 to/from 地图。