Can JAXB incrementally marshall an object?

I have a fairly simple but potentially large structure for serialization. Basically the XML structure would be:

     ... main_object_type repeats up to 5,000 times



can contain a significant amount of data. On my first checkout of 3500 records, I had to give the JVM more memory than I needed.

So, I would like to write to disk after each (or several) main_object_type


I know the installation Marshaller.JAXB_FRAGMENT

will allow snippets to be used, but I am missing the external document tags xml and <simple_wrapper>


Any suggestions?


source to share

2 answers

How about the next one?

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

// 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


This assumes you have @XmlRootElement in MainObjectType

import javax.xml.bind.annotation.XmlRootElement;

public class MainObjectType {




You can marshal your object to a SAX or StAX stream.



All Articles