How to add comments for JAXB

I’d be surprise if jaxb doesn’t support adding comment <!-- –> when generating XML. Did anyone had success??? I’d really hate doing replace method to add some XML comments…

One thing you could do is to use Marshal Event Callbacks(https://jaxb.dev.java.net/nonav/2.1.4/docs/api/javax/xml/bind/Marshaller.html#marshalEventCallback) to get access to the marshalled objects before/after marshalling and then output some info about them into the same file.

Create a new listener on the marshaller, override the “beforeMarshal” method, and write something to the writer passed in to the marshaller’s “marshal” method.

Thanks for the suggestion. That would definitely work. For now, I just put a gigantic comment in the beginning of the XML. Still, I’m a bit surprise on how cumbersome to add comment through that way… ah well~ I’d still use jaxb though!