Java XStream fromXML() 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1001899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:07:31  来源:igfitidea点击:

XStream fromXML() exception

javaexceptionxml-serializationxstream

提问by

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.

我正在尝试使用 XStream 包在 Java 中反序列化一个字符串。XStream 包可以很好地序列化我的类。我从服务器获取 XML(无法更改 XML 的格式)并尝试将其节点信息保存到某个类中的相应变量中。我的函数在底部,我试图为 XStream 对象注册一个新的转换器(认为这是因为一个变量是一个字节数组),但仍然没有运气。任何人都可以对这些例外有所了解吗?我是否需要注册“MyClass”并为 XStream 编写自己的转换器来处理我的类的反序列化?提前致谢。

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

如果将字符串或 StringReader 对象作为输入传入 fromXML() ,则出现异常:

[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)

[致命错误] :1:1: prolog 中不允许出现内容。
com.thoughtworks.xstream.io.StreamException:: 内容在序言中是不允许的。
在 com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:86)
在 com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:66)
在 com.thoughtworks.xstream.XStream .fromXML(XStream.java:853)

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

如果 ByteArrayInputStream 用作 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)

com.thoughtworks.xstream.converters.ConversionException: ByteSize : ByteSize : ByteSize : ByteSize
---- 调试信息----
消息: ByteSize : ByteSize
原因异常: com.thoughtworks.xstream.mapper.CannotResolveClassException
原因消息: ByteSize : ByteSize
类 : MyClass
必需类型 : MyClass
路径 : /MyClass/ByteSize
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:89)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.convert (TreeUnmarshaller.java:89) java:63)
在 com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
在 com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
在 com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
在 com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.un (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);
}

回答by matt b

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

看看这个问题:content not allowed in prolog exception

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

“序言中不允许的内容”通常意味着<?xml文件中标题(“序言”)之前有一些内容。这是不允许的。

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

因此,请检查以确保<?xml字符串中没有前面的字符,并且您没有任何BOM问题。

回答by Brian Agnew

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

您的反序列化/解码 XStream 实例是否以编码 XStream 实例相同的方式配置?我会检查后者,并确保相同的 XStream 实例可以编码/解码。

回答by Assaf Israel

This is an encoding issue. From the XStream documentation:

这是编码问题。从 XStream文档

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

“自 1.3 版以来,所有 HierarchicalStreamDriver 实现都尊重编码,但前提是您提供 InputStream。”

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

当您尝试读取 XML 时,只需添加一个 Reader。例如:

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