0

I am trying to covert an xml to object using xstream:-

  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)

The testing class has been defined as follows:-

class testing {

  private var note = None;

}

I am getting the following error:-

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)

Please help with the above.

Also is there a way to directly convert the xml into a map in scala?

2
  • I think you should provide a constructor for the Testing class which takes the note as a parameter. Commented Aug 28, 2016 at 11:27
  • i did that now the testing class looks like this:- class testing(note:String) { def getNote = note; } but i get error on calling convertedMap.getNote java.lang.NoSuchMethodError: testing.getNote()Ljava/lang/String; at #worksheet#.convert(sum.sc:26) at #worksheet#.#worksheet#(sum.sc:29) Commented Aug 28, 2016 at 12:05

1 Answer 1

1

I have no problem running your code:

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
}

Perhaps your testing class is not in the default package, so it is a packaging issue?

Anyway, how do you expect a variable of None type to store your data? Perhaps you should make it a String or Int. I am not familiar with Xstream, but if you wanted the library to map directly xml into Option, I think you will need to write a custom converter. This applies to converting to/from Maps too.

Sign up to request clarification or add additional context in comments.

4 Comments

They are in the same package. But different files. Adding the constructor helped resolve the issue but getting a different error as stated in above comment.
Also are you able to access the note variable using some getter?
Yes, surely. My Scala version is 2.11.8, by the way.
Out of curiosity, what was your problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.