mr.nothing mr.nothing - 3 months ago 27
Java Question

xstream primitive deserialization doesn't work?

I'm using XStream serializer to serialize a primitive and faced problem with deserializing it. Here is the problem description:

Say, I have:

int i = 80;


I serialize it as following:

serializer.toXML(i, new FileOutputStream("me.xml"));


and get the this xml:

<int>80</int>


But when I'm trying to deserealize:

i = (int) serializer.fromXML("me.xml");


I get the exception like this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at com.thoughtworks.xstream.core.util.FastStack.pop(FastStack.java:42)
at com.thoughtworks.xstream.io.xml.AbstractPullReader.move(AbstractPullReader.java:125)
at com.thoughtworks.xstream.io.xml.AbstractPullReader.moveDown(AbstractPullReader.java:103)
at com.thoughtworks.xstream.io.xml.XppReader.<init>(XppReader.java:63)
at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:54)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
at Utilities.Parser.serializeTimeIntervalDuration(Parser.java:371)
at Utilities.Parser.parseData(Parser.java:152)
at CoreLayer.Main.Main.main(Main.java:52)


Could anybody point me on my mistakes?

Thanks in advance!

Answer

That's because fromXML() expects the XML, not a filename. You should read your XML in from the file prior to parsing it.

Note that XStream has multiple implementations of fromXML(), so you can provide strings, URLs, readers etc.

At the moment XStream is trying to make sense of the string "me.xml".