Format XML with JAXB during unmarshal
Asked Answered
F

4

11

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!

Function answered 2/7, 2009 at 12:28 Comment(5)
does unmarshall not result in an object instance?Ovenware
Then, what do you want to format?Basalt
"but I want to do it with JAXB!" - Is there a good reason to "want" it this way? I assume now that you mean "Format XML with JAXB during MARSHALLING": - XML output is there to be transformed, one could say, it is its primary reason of existence ;) And there are 1000 possibilities to do this. So just transform the generated XML in a way you want.Genip
Why this question has one point if it is senseless?Delagarza
Beside it makes no sence setting the same property like above to the unmarshaller will result in a Exception: javax.xml.bind.PropertyException: name: jaxb.formatted.output value: true (METRO was used for the test).Efrem
A
43

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

Archy answered 24/9, 2010 at 13:34 Comment(2)
This sounds reasonable, but one could want to log the xml code as a string to a file in a controlled environment.Enumeration
It is also helpful to get error which is on specific line. If it is not formatted, eg: at line 22, column 22. If not formatted it will just show at line 1, column 220Photogene
B
26

If you want to log formatted XML corresponding to the XML that you just unmarshalled, you can simply remarshal the unmarshalled object back to XML, using the property you specified, ie.

/**
 * Marshall input object to a formatted XML String
 */
protected <T> String marshal(T input) throws JAXBException {
    StringWriter writer = new StringWriter();

    JAXBContext jc = JAXBContext.newInstance(input.getClass());
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(input, writer);
    return writer.toString();
}

On the other hand, if all you want to do is reformat the XML, you probably should be using JAXP instead of JAXB.

Brotherly answered 18/3, 2013 at 9:8 Comment(0)
S
3

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.

Stearns answered 14/9, 2010 at 6:2 Comment(0)
A
0

One way to do this, if you insist, is to use an XSLT Transformer, such as Saxon's, that supports "teeing," i.e. lets you transform a Source to two Result objects. I don't know why you call String#getBytes(); you should be creating a StringReader and pulling from that. The two destinations for your "tee" would be the "identity transform" (the default if you call TransformerFactory#newTransformer()) and the other would be JAXBResult.

Aga answered 15/5, 2011 at 19:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.