用于序列化的xstream错误&反序列化 [英] xstream errors for serializing & deserializing

查看:207
本文介绍了用于序列化的xstream错误&反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中使用xStream来序列化java库中的java对象并在客户端对其进行反序列化。

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

我有几个问题:

如果我这样做:

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

=>序列化没问题,但反序列化:线程maincom中的异常.thoughtworks.xstream.io.StreamException ::在开始标记之前只允许空格内容,而不是。 (位置:START_DOCUMENT见过...... @ 1:1)

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

如果我这样做:

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

=>我遇到了序列化问题:线程maincom中的异常。 thoughtworks.xstream.io.StreamException ::在开始标记之前只允许空格内容,而不是。 (位置:START_DOCUMENT见.... @ 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)

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

使用xml:

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

那么有什么建议吗?

谢谢提前。

推荐答案

所以这里忽略的是你如何阅读文件。你正在使用

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

这是句点(。)来自错误的地方。 fromXML的方法是期望实际的XML输入而不是文件名。因此,当它解析你的xml(它是model.xml而不是实际的xml)时,它会给出错误。 XStream的站点现在已经关闭,所以我无法链接到API

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

使用FileReader / BufferedReader来获取XML的内容。像这样的东西应该工作

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

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。我能够复制问题,并使用上面的

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

这篇关于用于序列化的xstream错误&amp;反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆