views:

1201

answers:

3

Hi there,

I want to format a XML document during unmarshal with JAXB. Unmarshal looks like:

Unmarshaller u = createAndsetUpUnmarshaller(enableValidation, evtHandler, clazz);
return u.unmarshal(new ByteArrayInputStream(stringSource.getBytes()));

While marshaling one can format the code via:

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

But this isn´t possible for the unmarchal process... Any idea how I can format the XML string with JAXB during (or after) unmarshal process?

BTW: I read some posts here about pretty print, but I want to do it with JAXB!

A: 

Very good, bois...

Michel
A: 

I think there is no pretty print for Unmarshaller because the result of the JAXB unmarshaller is not an XML but a Java object. If you want to pretty print the resulting unmarshalled object better override the toString() method of the jaxb generated object. (This will be a messy solution since each time you generate the JAX binding classes you will haveto introduce the toString() method yourself.

Hmmm... I hope the future versions of JAXB will have a solution to this shortcoming, since it is important for logging, etc.

Isuru Sampath
+2  A: 

it is logically senseless to format the xml code while unmarshalling it?

subes