【发布时间】:2011-05-30 11:48:46
【问题描述】:
我在 Java 中使用 xStream 来序列化 java 库中的 java 对象并在客户端反序列化它。
我有几个问题:
如果我这样做:
XStream xstream = new XStream();
xstream.setMode(XStream.ID_REFERENCES);
xstream.autodetectAnnotations(true);
Writer writer = new FileWriter(xmlFile);
writer.write(xstream.toXML(myObject));
writer.close();
=> 序列化没问题,但反序列化: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();
=> 我遇到了序列化问题: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">
那么有什么建议吗?
提前致谢。
【问题讨论】:
-
能否请您发布您正在序列化的课程?
-
嗨,正如我所提到的,我没有类结构,它在 java 库中。
-
看看这是否有帮助 stackoverflow.com/questions/1524775/error-reading-settings-xml 似乎有一个句号(。)造成严重破坏
-
我检查了我的 xml 输出,没有 .在文档中!
-
您能否将 xmlFile 打印到其他输出并在答案中发布?并发布更多与异常相关的堆栈跟踪