我使用XStream和DomDriver来序列化和反序列化DefaultStyledDocument对象,因此我在数据库中保存和检索它的状态。序列化部分进行得很好,但是当它试图反序列化时,会抛出一个excetption:
[Fatal Error] :92:51: Character reference "&# Exception in thread "AWT-EventQueue-0" com.thoughtworks.xstream.io.StreamException: : Character reference "&#
我相信&#是文档中的空格字符。
我甚至尝试使用不同的驱动程序,比如:StaxDriver、JsonHierarchicalStreamDriver和JettisonMappedXmlDriver,但都没有成功。
我在这里做错什么了?
这是我的密码:
DefaultStyledDocument doc = new DefaultStyledDocument();
//initialize doc
XStream xmlstream = new XStream(new StaxDriver());
String xml = xmlstream.toXML(doc);
//save 'xml' in database
//select from database
DefaultStyledDocument document = (defaultStyledDocument) xmlstream.fromXML(result.getString(1));
//this is where the exception is thrown.编辑实际上&#不是空格字符,因为现在我看到空格字符是由“”表示的。&#似乎是文档中未经编辑(空)的部分。序列化它,但不将它反序列化回来,这真的很烦人。
发布于 2012-10-24 18:00:33
考虑到您在上面的注释中提供的XML片段,错误消息是正确的--规范禁止某些字符出现在XML1.0文档中,甚至是作为字符引用,而U+0000就是这些字符之一。因此,�不是格式良好的XML,解析器拒绝它是正确的。序列化程序在允许编写序列化程序时显然比较宽松。
我建议您探索在数据库中表示这些数据的其他非XML方法,比如BLOB (使用Java对象序列化)或类似的方法。
https://stackoverflow.com/questions/13054594
复制相似问题