Can JAXB incrementally marshall an object?

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

<simple_wrapper>
   <main_object_type>
     <sub_objects>
   </main_object_type>
     ... main_object_type repeats up to 5,000 times
</simple_wrapper>

      

main_object_type

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?

0


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
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 @XmlRootElement in MainObjectType

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MainObjectType {
    ...
}

      

+2


source


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



0


source







All Articles