【问题标题】:XStream does not deserialize back from XMLXStream 不会从 XML 反序列化返回
【发布时间】:2012-10-14 19:27:17
【问题描述】:

我使用XStreamDomDriver 来序列化和反序列化DefaultStyledDocument 对象,所以我在数据库中保存和检索它的状态。序列化部分运行良好,但是当它尝试反序列化时,抛出异常:

[Fatal Error] :92:51: Character reference "&# Exception in thread "AWT-EventQueue-0" com.thoughtworks.xstream.io.StreamException: : Character reference "&#

我相信 是文档中的空格字符。

我什至尝试使用不同的驱动程序,例如:StaxDriverJsonHierarchicalStreamDriverJettisonMappedXmlDriver,但没有成功。

我在这里做错了什么?

这是我的代码:

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.

编辑 实际上 不是空格字符,因为现在我看到空格字符由“”表示。 似乎是文档的未编辑(空)部分。序列化它,但不反序列化它,真的很烦人。

【问题讨论】:

  • 你能展示一个 XML 的例子吗?
  • 或者至少是错误周围的位(第 92 行)。
  • @IanRoberts www.google.com & #x0;... 这持续了很长一段时间,然后有标准的结束标签。
  • 那么错误信息是正确的——� 在 XML 1.0 中是不合法的。看起来序列化程序在允许写入时很宽容,但解析器(正确地)拒绝它,因为它在读取时格式错误。我建议您为此查看一个非 XML 解决方案,可能是 ObjectOutputStreamByteArrayOutputStream 并将结果字节存储为 BLOB。
  • @IanRoberts 你认为 JSON 会起作用吗?

标签: java xml xml-serialization xstream


【解决方案1】:

鉴于您在上面的 cmets 中提供的 XML sn-p,错误消息是正确的 - 规范禁止在 XML 1.0 文档中出现某些字符即使作为字符引用, U+0000 是这些字符之一。因此� 不是格式良好的 XML,解析器拒绝它是正确的。序列化程序显然更宽松地允许编写它。

我建议您探索其他非 XML 方式来表示数据库中的这些数据,可能是 BLOB(使用 Java 对象序列化)或类似方式。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
  • 2013-03-24
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多