Igor
Igor

Reputation: 1582

XStream does not deserialize back from XML

I use XStream with DomDriver to serialize and deserialize a DefaultStyledDocument object, so I save and retrieve it's state in a database. The serialization part goes well, but when it tries to deserialize, an excetption is thrown:

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

I believe &# is the space character in the document.

I even tried using a different driver like: StaxDriver, JsonHierarchicalStreamDriver and JettisonMappedXmlDriver, but had no luck.

What am I doing wrong here?

Here's my code:

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.

EDIT Actually &# is not a space character, because now i see that the space character is represented by " ". &# seems to be the not edited (empty) part of the document. It's really annoying that is serializes it, but doesn't deserialize it back.

Upvotes: 2

Views: 1576

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122364

Given the XML snippet you provided in the comments above, the error message is correct - there are certain characters that are forbidden from appearing in XML 1.0 documents by the spec even as character references, and U+0000 is one of these characters. Thus � is not well-formed XML and the parser is right to reject it. The serializer is clearly being more lenient in allowing it to be written.

I would suggest you explore other non-XML ways to represent this data in your database, maybe as a BLOB (using Java object serialization) or similar.

Upvotes: 2

Related Questions