简体   繁体   English

xstream原始反序列化不起作用?

[英]xstream primitive deserialization doesn't work?

I'm using XStream serializer to serialize a primitive and faced problem with deserializing it. 我正在使用XStream序列化程序来序列化原始的和反序列化所面临的问题。 Here is the problem description: 这是问题描述:

Say, I have: 说,我有:

int i = 80;

I serialize it as following: 我将其序列化如下:

serializer.toXML(i, new FileOutputStream("me.xml"));  

and get the this xml: 并获取以下xml:

<int>80</int>

But when I'm trying to deserealize: 但是,当我试图使现实化时:

i = (int) serializer.fromXML("me.xml");

I get the exception like this: 我得到这样的异常:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at com.thoughtworks.xstream.core.util.FastStack.pop(FastStack.java:42)
    at com.thoughtworks.xstream.io.xml.AbstractPullReader.move(AbstractPullReader.java:125)
    at com.thoughtworks.xstream.io.xml.AbstractPullReader.moveDown(AbstractPullReader.java:103)
    at com.thoughtworks.xstream.io.xml.XppReader.<init>(XppReader.java:63)
    at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:54)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)
    at Utilities.Parser.serializeTimeIntervalDuration(Parser.java:371)
    at Utilities.Parser.parseData(Parser.java:152)
    at CoreLayer.Main.Main.main(Main.java:52)  

Could anybody point me on my mistakes? 有人能指出我的错误吗?

Thanks in advance! 提前致谢!

That's because fromXML() expects the XML, not a filename. 这是因为fromXML()需要XML,而不是文件名。 You should read your XML in from the file prior to parsing it. 您应该在解析文件之前从文件中读取XML。

Note that XStream has multiple implementations of fromXML(), so you can provide strings, URLs, readers etc. 请注意, XStream具有fromXML()的多种实现,因此您可以提供字符串,URL,阅读器等。

At the moment XStream is trying to make sense of the string "me.xml". 目前,XStream试图理解字符串“ me.xml”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM