I\'ve got a fairly simple, but potentially large structure to serialize. Basically the structure of the XML will be:
You can marshal your object into a SAX or StAX stream.
How about the following?
JAXBContext jaxbContext= JAXBContext.newInstance(MainObjectType.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
OutputStreamWriter writer = new OutputStreamWriter(System.out);
// Manually open the root element
writer.write("<simple_wrapper>");
// Marshal the objects out individually
marshaller.marshal(mainObjectType1, writer);
marshaller.marshal(mainObjectType2, writer);
marshaller.marshal(mainObjectType3, writer);
marshaller.marshal(mainObjectType4, writer);
...
// Manually close the root element
writer.write("</simple_wrapper>");
writer.close();
This assumes you have an @XmlRootElement on MainObjectType
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MainObjectType {
...
}