views:

4056

answers:

2

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);
}
+3  A: 

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.

matt b
You are correct. when i print out the string input being passed in there are three junk characters before the <?xml.
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
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")
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.html
matt b
I will look more into this option. Thanks for your help on clearing up the prolog exception.
A: 

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.

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 .net
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
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<root><data1>1</data1><data2>true</data2><data3>Example Text</data3>...</root>
XStream will need to map that to a Java object (possibly called root) with data fields data1/data2 etc.
Brian Agnew
Do you know of any clear examples of setting up a new map for an XStream object