JAXB: Marshal output XML with indentation create empty line break on the first line

As you point out EclipseLink JAXB (MOXy) does not have this problem so you could use that (I'm the MOXy lead):

  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

Option #1

One option would be to use a java.io.FilterWriter or java.io.FilterOutputStream and customize it to ignore the leading new line.

Option #2

Another option would be to marshal to StAX, and use a StAX implementation that supports formatting the output. I haven't tried this myself but the answer linked below suggests using com.sun.xml.txw2.output.IndentingXMLStreamWriter.

  • https://stackoverflow.com/a/3625359/383861

Tags:

Java

Jaxb

Moxy