JAXB on-demand unmarshalling

120 Views Asked by At

Say I have a very large XML file to unmarshal, like 2 MB. I don't want to unmarshal the file and end up with (aprox.) 2 MB of objects in memory.

Instead, I want to unmarshal portions of the file on-demand (i.e. as I navigate through the object structure) and cleanup the references of the objects I have already read, making them available to the garbage collector.

Is it possible to tweak the JAXB implementation in a way it unmarshals the XML on-demand instead of entirely at once?

2

There are 2 best solutions below

0
On BEST ANSWER

Take a look in this solution: Partial Unmarshalling of an XML using JAXB to skip some xmlElement

A specialist class was created to provide a mechanism to read the xml in parts.

0
On

A comprehensive example can be found here.

It uses StAX to advance to the right position within the xml file and JAXB to unmarshal the domain object.