Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format XML with JAXB during unmarshal

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!

like image 787
Tobias Avatar asked Jul 02 '09 12:07

Tobias


People also ask

How do you Unmarshal XML string to Java object using JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

How does JAXB read XML?

To read XML, first get the JAXBContext . It is entry point to the JAXB API and provides methods to unmarshal, marshal and validate operations. Now get the Unmarshaller instance from JAXBContext . It's unmarshal() method unmarshal XML data from the specified XML and return the resulting content tree.

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.


1 Answers

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

like image 181
subes Avatar answered Oct 23 '22 08:10

subes