JAXB generates unformatted XML using XMLEventWriter

1k Views Asked by At

I'm using JAXB in order to generate XML files, and due to a business need I'm currently writing it to the middle of some other XML file using XMLEventWriter:

marshaller.marshal(jaxbElement, xmlEventWriter);

And currently setting some properties like:

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

But, besides having the JAXB_FORMATTED_OUTPUT set to true, my XML is not being formatted.

Does anyone knows what may be the problem? This only happens when I use the XMLEventWriter...

Thanks in advance.

2

There are 2 best solutions below

0
On

When you are using an XMLEventWriter as a sink, the JAXB marshaller is only in charge of sending the appropriate XML events to it and the XMLEventWriter may still choose to write out unformatted XML. My advice is to check the configuration of your XMLEventWriter in addition to Marshaller.

0
On

Unfortunately, the default XmlEventWriter implementation does not indent. The stax-utils library provides a IndentingXMLEventWriter which might help in these cases.