rjsang rjsang - 1 year ago 156
Java Question

How to prevent JAXB escaping a string

I have an object, generated by XJC, called

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

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download