java 序列化和反序列化的 xstream 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6175883/
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-10-30 14:41:36  来源:igfitidea点击:

xstream errors for serializing & deserializing

javaxstream

提问by olidev

I am using xStream in Java to serialize a java object from a java library and deserializing it at the customer's side.

我在 Java 中使用 xStream 从 Java 库序列化 Java 对象并在客户端反序列化它。

I have several problems:

我有几个问题:

If I do it like this:

如果我这样做:

XStream xstream = new XStream();
xstream.setMode(XStream.ID_REFERENCES);
xstream.autodetectAnnotations(true);
Writer writer = new FileWriter(xmlFile);        
writer.write(xstream.toXML(myObject));
writer.close();

=> serializing is OK but deserializing: Exception in thread "main" com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not . (position: START_DOCUMENT seen .... @1:1)

=> 序列化没问题,但反序列化: Exception in thread "main" com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not . (position: START_DOCUMENT seen .... @1:1)

if I do it like this:

如果我这样做:

XStream xstream = new XStream();
xstream.setMode(XStream.NO_REFERENCES);
xstream.autodetectAnnotations(true);
Writer writer = new FileWriter(xmlFile);        
writer.write(xstream.toXML(myObject));
writer.close();

=> I got serialization problem: Exception in thread "main" com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not . (position: START_DOCUMENT seen .... @1:1) at com.thoughtworks.xstream.io.xml.XppReader.pullNextEvent(XppReader.java:78) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readRealEvent(AbstractPullReader.java:137) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readEvent(AbstractPullReader.java:130) at com.thoughtworks.xstream.io.xml.AbstractPullReader.move(AbstractPullReader.java:109) at com.thoughtworks.xstream.io.xml.AbstractPullReader.moveDown(AbstractPullReader.java:94) at com.thoughtworks.xstream.io.xml.XppReader.<init>(XppReader.java:48) at com.thoughtworks.xstream.io.xml.XppDriver.createReader(XppDriver.java:44) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845)

=> 我遇到了序列化问题: Exception in thread "main" com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not . (position: START_DOCUMENT seen .... @1:1) at com.thoughtworks.xstream.io.xml.XppReader.pullNextEvent(XppReader.java:78) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readRealEvent(AbstractPullReader.java:137) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readEvent(AbstractPullReader.java:130) at com.thoughtworks.xstream.io.xml.AbstractPullReader.move(AbstractPullReader.java:109) at com.thoughtworks.xstream.io.xml.AbstractPullReader.moveDown(AbstractPullReader.java:94) at com.thoughtworks.xstream.io.xml.XppReader.<init>(XppReader.java:48) at com.thoughtworks.xstream.io.xml.XppDriver.createReader(XppDriver.java:44) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845)

With xml:

使用 xml:

<Test.Platform id="1">
    <TaskImpl id="1">
          <model reference="2"/>
          <name>process</name>
    </TaskImpl>
</Test.Platform id="1">

So Any suggestion?

所以有什么建议吗?

Thanks in advance.

提前致谢。

回答by Sean

so the thing that is overlooked here is how you are reading in the file. you are using

所以这里被忽视的是你在文件中的阅读方式。您正在使用

XStream xstream = new XStream();
xstream.fromXML("model.xml");

Which is where the period(.) is coming from in the error. The method fromXML is expecting the actual XML input and not the file name. So when it parses your xml (which is "model.xml" not the actual xml) it is giving the error. The site for XStream is down right now so I can't link to the API

这就是错误中句号(.) 的来源。fromXML 方法需要实际的 XML 输入,而不是文件名。所以当它解析你的 xml(它是“model.xml”而不是实际的 xml)时,它给出了错误。XStream 的站点目前已关闭,因此我无法链接到 API

Use a FileReader/BufferedReader in order to get the contents of the XML back. Something like this should work

使用 FileReader/BufferedReader 来取回 XML 的内容。这样的事情应该工作

XStream instream = new XStream();

BufferedReader br = new BufferedReader(new FileReader("model.xml"));
StringBuffer buff = new StringBuffer();
String line;
while((line = br.readLine()) != null){
   buff.append(line);
}
Platform p = (Platform)instream.fromXML(buff.toString());

P.S. I was able to duplicate the problem, and fix it with the above

PS我能够复制这个问题,并用上面的方法修复它