I have some objects let\'s say two, A and B. These objects from the same class. I need to marshal these objects using JAXB and the output XML should be in this form:
Try this:
import java.io.StringWriter;
import javax.xml.bind.Marshaller;
...
Object requestObject = ... // This is the object that needs to be printed with indentation
Marshaller marshaller = ...
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter stringWriter = new StringWriter();
marshaller.marshal(requestObject, stringWriter);
System.out.println(stringWriter.toString());