I've tried to read a big xml file (something like 500MB). First of all, I used xjc with the XSD file of my XML. All classes were generated as expected. Trying to read the file I've got this error: javax.xml.bind.UnmarshalException: unexpected element.
Here is my code:
(...)
JAXBContext context = JAXBContext.newInstance("br.com.mypackage");
Unmarshaller unmarshaller = context.createUnmarshaller();
File f = new File("src/files/MyHuge.CNX");
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
InputStream in = new FileInputStream(f);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
Person p = null;
int count = 0;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
if (startElement.getName().getLocalPart() == ("person")) {
p = (Person) unmarshaller.unmarshal(eventReader);
}
}
}
The problem is in the unmarshal operation.
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"identification"). Expected elements are <{}messageAll>
I used this link as example to make my own code: JAXB - unmarshal OutOfMemory: Java Heap Space
Someone has a clue to do it? All that I want now is to read a huge XML file without unmarshal the external object of XML (java heap space problem) and without reading tag by tag getting the respective value, a slow and monkey code (not the monkeys of Rise of the Planet of the Apes). :P
Many thanks.