Java – XStream fromXML() exception

exceptionjavaxml-serializationxstream

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);
}

Best Answer

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.