mr.nothing mr.nothing - 1 year ago 145
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:


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(
at com.thoughtworks.xstream.XStream.fromXML(
at com.thoughtworks.xstream.XStream.fromXML(
at Utilities.Parser.serializeTimeIntervalDuration(
at Utilities.Parser.parseData(
at CoreLayer.Main.Main.main(

Could anybody point me on my mistakes?

Thanks in advance!

Answer Source

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".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download