users@jaxb.java.net

Re: Handling Special Chars

From: Robert Lowe <robertmlowe_at_rmlowe.com>
Date: Mon, 03 Oct 2005 05:16:04 +0800

Ralf Kistner wrote:

> I sometimes have a problem with JAXB that it cannot even read an XML
> file that it just wrote... I think it is a problem with JAXB not
> handling special characters like "ê" and "ë". I'm not sure about this
> though.

Hmm, JAXB creates documents using the UTF-8 character encoding by
default, so it should be able to handle any Unicode character without
problems. I've personally used JAXB 1.0 to create JAXB documents
containing Chinese and Japanese characters as well as accented Roman
characters, so I believe JAXB is solid in this regard.

I suspect it's a problem with the way you're using JAXB rather than a
problem with JAXB itself--for example, marshalling to a Writer, and then
encoding this using an encoding that doesn't match the encoding used in
marshalling. For example, the code below will almost certainly produce
incorrect output:

FileWriter writer = new FileWriter("myfile.xml");
marshaller.marshal(myJaxbObject, writer);

The problem here is that, by default, JAXB will include an XML
declaration something like:

<?xml version="1.0" encoding="UTF-8" ?>

However, the FileWriter uses the platform-specific default character
encoding, which almost certainly *won't* be UTF-8, resulting in a
mismatch between the declared encoding and the actual encoding. The safe
way to do this would be:

FileOutputStream os = new FileOutputStream("myfile.xml");
marshaller.marshal(myJaxbObject, os);

If you still think there's a problem with JAXB, I'd suggest you create a
simple test case, and then submit this to the mailing list together with
sample output.

-- 
Best regards,
Robert Lowe
http://rmlowe.com/