tags:

views:

127

answers:

3

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?

+1  A: 

i think there is no way cuz without escaping you xml document will not be valid. However i don't have much experience with xml and jaxb.

foret
But what I'm saying is that I've already escaped it. I don't need JAXB to do it for me again!
Rob
+1  A: 

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

Okay, that's great, but if I do that I am forced to write an implementation that covers how to escape every single character. How do I delegate to the default behaviour if the character is not the one I'm interested in?
Rob
I'm accepting this as the correct answer, because it SHOULD be the correct answer. For anyone else reading this hoping it will help you, this doesn't actually work in JAXB 2.2.1 because of a bug ( https://jaxb.dev.java.net/issues/show_bug.cgi?id=777 )
Rob
A: 

How about using a CDATA section to wrap the value, and then use a JAXB implementation such as EclipseLink MOXy that can handle CDATA?

To handle CDATA using MOXy see:

Blaise Doughan