XStream fromXML () exception

I am trying to deserialize a string in Java using the XStream package. The XStream package can serialize my class. I get XML (I can’t change the XML format) from the server and try to save its node information in the appropriate variables in a specific class. My function is at the bottom, and I tried to register a new converter for the XStream object (assuming that this is because one variable is an array of bytes), but still no luck. Can anyone shed light on these exceptions? Do I need to register "MyClass" and write my own converter for XStream to deserialize my class? Thanks in advance.

Exception if a string or StringReader is passed from XML () as input:

[Fatal error]: 1: 1: Content is not allowed in the prolog.
com.thoughtworks.xstream.io.StreamException: Content is not allowed in the 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 fromXML ():

com.thoughtworks.xstream.converters.ConversionException: ByteSize: ByteSize: ByteSize: ByteSize
---- Debug information ---- message: ByteSize: ByteSize
reason-exception: com.thoughtworks.xstream.mapper.CannotResolveClassException
reason-message: ByteSize : ByteSize
class: MyClass
type required: MyClass
path: / MyClass / ByteSize
    on com.thoughtworks.xstream.core.TreeUnmarshaller.convert (TreeUnmarshaller.java:89)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert (AbstractReferenceUnmarshaller : 63)
    on com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother (TreeUnmarshaller.java:76)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert and another (TreeUnmarshaller.java:60)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start (TreeUnmarshaller.java:137)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrate. 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
source share
3 answers

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

" " , <?xml ( "" ) . .

, , <?xml, BOM .

+4

. XStream :

" HierarchicalStreamDriver 1.3, InputStream."

Reader, XML. :

Object obj = xStream.fromXML(new FileReader(xmlFile));
+4

Is your deserialization / decoding XStream instance configured to the same as your XStream instance for encoding? I would check the latter and ensure that the same XStream instance can encode / decode.

0
source

Source: https://habr.com/ru/post/1710583/


All Articles