What does your XML look like? Generally, for large documents, I recommend that people use the StAX XMLStreamReader so that the document can be unarmalized by JAXB in pieces.
Input.xml
The document below has many instances of the person element. We can use JAXB with the StAX XMLStreamReader to untie the corresponding person objects one at a time to avoid running out of memory.
<people> <person> <name>Jane Doe</name> <address> ... </address> </person> <person> <name>John Smith</name> <address> ... </address> </person> .... </people>
Demo
import java.io.*; import javax.xml.stream.*; import javax.xml.bind.*; public class Demo { public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); xsr.nextTag();
Person
Instead of matching the root element of the XML document, we need to add @XmlRootElement annotations to the local root of the XML fragment from which we will disconnect.
@XmlRootElement public class Person { }
Blaise donough
source share