rjsang rjsang - 13 days ago 5
Java Question

How to prevent JAXB escaping a string

I have an object, generated by XJC, called

Product
. I want to set
product.currentPrice (a String)
to be
£210
where
£
is the currency symbol (passed in from elsewhere in the system).

Trouble is, JAXB is escaping my ampersand, so it produces
£210
instead. How do I make it not do this?

Answer

By default, the marshaller implementation of the JAXB usually escapes characters. To change this default behavior you will have to Write a class that implements the com.sun.xml.bind.marshaller.CharacterEscapeHandler interface.

set that handler in the Marshaller

CharacterEscapeHandler escapeHandler = NoEscapeHandler.theInstance;
marshaller.setProperty("com.sun.xml.bind.characterEscapeHandler", escapeHandler); 

You can have a look at samples provided with JAXB, character escaping sample

Comments