XMLEncoder and Non-XML Characters

XMLEncoder does not appear to properly handle Strings containing certain characters which are invalid in XML documents. It can handle quotes and whatnot just fine, but the following code

PipedInputStream pis =new PipedInputStream();

PipedOutputStream pos =new PipedOutputStream(pis);

XMLEncoder encoder =new XMLEncoder(pos);

encoder.writeObject("test" + (char) 0);

encoder.close();

XMLDecoder decoder =new XMLDecoder(pis);

System.out.println(decoder.readObject());

produces this exception

org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the element content of the document.

Continuing ...

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

and thus the string is lost.

I was considering using XMLEncoder/XMLDecoder as an alternative to object stream serialization because the latter has trouble handling field renames and whatnot (it explodes quite violently if you deserialize a stream containing an enum value you have since removed). XMLEncoder/XMLDecoder seemed to have better error handling in that errors didn't fatalize the stream; you could recover, be notified and so on. However, discovering it can't handle a very fundamental class makes me a bit more than nervous about using it.

Any suggestions?

[1551 byte] By [tvynra] at [2007-10-2 16:52:55]
# 1

No time to test this at the moment, but maybe you can consider overriding the persistence delegate for Strings, which will encode it to legal XML. Hopefully when it's read back in it will be what you put in in the first place (not convinced, might play with it later)

I'd consider submitting this as a bug report, especially if you can make XMLEncoder break on classes which the javadoc claims will use XMLEncoder for long time persistence in future (e.g. javax.swing.JLabel, can you set a funky string on that which is legal for a JLable, but breaks XMLEncoder?)

sorabaina at 2007-7-13 18:04:53 > top of Java-index,Core,Core APIs...
# 2
I'm not sure where I'd find a list of classes which are supposedly supported by XMLEncoder. Is it sufficient that a Map<String,String> where a key or value contains a null character breaks it?
tvynra at 2007-7-13 18:04:54 > top of Java-index,Core,Core APIs...
# 3
Oh, and as a note, it doesn't read back in properly. The XML parser chokes and the object disappears into the void.
tvynra at 2007-7-13 18:04:54 > top of Java-index,Core,Core APIs...
# 4
I took the example code in the class-level API doc of XML{En,De}coder subject to a two-character change which inserted a \0 into the string, and got it to break. That convinces me it's worth submitting as a bug report.
YAT_Archivista at 2007-7-13 18:04:54 > top of Java-index,Core,Core APIs...
# 5
k, will do. Thanks. :)
tvynra at 2007-7-13 18:04:54 > top of Java-index,Core,Core APIs...