XStream fromXML() exception

Go To StackoverFlow.com


I am trying to deserialize a string in Java using the XStream package. The XStream package can serialize my class fine. I get the XML (cannot change format of XML) from a server and try to save its node information to the corresponding variables in a certain class. My function is at the bottom and I tried to register a new converter for the XStream object (thinking that it was because one variable is a byte array) but still no luck. Can anyone shed some light on these exceptions? Do i need to register "MyClass" and write my own converter for XStream to handle deserializing my class? Thanks in advance.

Exception if a string or StringReader object are passed into fromXML() as input:

[Fatal Error] :1:1: Content is not allowed in prolog.
com.thoughtworks.xstream.io.StreamException: : Content is not allowed in prolog.
at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:86)
at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:66)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853)

Exception if ByteArrayInputStream is used as input to fromXML():

com.thoughtworks.xstream.converters.ConversionException: ByteSize : ByteSize : ByteSize : ByteSize
---- Debugging information ----
message : ByteSize : ByteSize
cause-exception : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message : ByteSize : ByteSize
class : MyClass
required-type : MyClass
path : /MyClass/ByteSize
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:89)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:861)

static Object fromXmlString(String xml) 
    XStream xStream = new XStream(new DomDriver());
    xStream.registerConverter(new EncodedByteArrayConverter());
    //tried all 3 below
    //return xStream.fromXML(new StringReader(xml));
    //return xStream.fromXML(new ByteArrayInputStream(xml.getBytes()));
    return xStream.fromXML(xml);
2009-06-16 14:36
by NoName


Take a look at this question: content not allowed in prolog exception.

"Content not allowed in prolog" usually means that there is some content before the <?xml header (the "prolog") in the file. This is not allowed.

So, check to make sure that there are no characters prior to <?xml in the string, and also that you do not have any BOM issues.

2009-06-16 14:42
by matt b
You are correct. when i print out the string input being passed in there are three junk characters before the NoName 2009-06-16 14:51
Ok so you simply need to make sure that content gets removed before you attempt to deserialize it. I would suggest you either do this yourself prior to invoking XStream, or if this data is coming from someone else, tell them to fix it - matt b 2009-06-16 14:56
I removed the junk characters on my end making sure the string "startsWith" the correct xml header. but now i get the ConversionException above even with the String as the input (where as the string previously was giving that "content not allowed in prolog" - NoName 2009-06-16 14:59
I believe this has to do with XStream not knowing what classes to match up with certain XML elements based on their name. You might want to take a look at registering aliases: http://xstream.codehaus.org/alias-tutorial.htm - matt b 2009-06-16 15:03
I will look more into this option. Thanks for your help on clearing up the prolog exception - NoName 2009-06-16 15:15


This is an encoding issue. From the XStream documentation:

"All HierarchicalStreamDriver implementations respect the encoding since version 1.3, but only if you provide an InputStream."

Simply add a Reader when you try to read the XML. For example:

Object obj = xStream.fromXML(new FileReader(xmlFile));
2012-04-02 16:04
by Assaf Israel
This worked for me. The problem I faced was when I am reading the XML file generated with the toXML function. When I wrapped the file with a FileReader, it XStream started reading the file properly - Sivaram Kannan 2013-06-19 09:01


Is your deserialising/decoding XStream instance configured in the same fashion as your encoding XStream instance ? I would check the latter, and ensure the same XStream instance can both encode/decode.

2009-06-16 14:44
by Brian Agnew
The XML string is actually being encoded in C#, so there really in no comparison i can make there. I believe they are using the XmlSerializer class provided by .ne - NoName 2009-06-16 14:48
I think you may have a lot of work ahead of you (depending on the XML you have to deserialise). I would expect XStream to assume that it's performed the serialisation as well. It may be worth posting the XML you have to deserialise - Brian Agnew 2009-06-16 14:56
There is nothing special about the XML. It is just a root node and about 25 child nodes of varying data types (byte array, string, int, boolean). I agree it may assume XStream serialized the data

Example XML it does not deserialize 1 true Example Text ... NoName 2009-06-16 15:03

XStream will need to map that to a Java object (possibly called root) with data fields data1/data2 etc - Brian Agnew 2009-06-16 15:06
Do you know of any clear examples of setting up a new map for an XStream objec - NoName 2009-06-16 15:14