Reputation: 81
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.
Upvotes: 2
Views: 4273
Reputation: 81
I solved the problem with this code bellow:
public List<Person> testeUnmarshal() {
List<Person> people = new ArrayList<Person>();
Person p = null;
try {
JAXBContext context = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
File f = new File(FILE_PATH);
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new FileInputStream(f));
while (eventReader.hasNext()) {
XMLEvent event = eventReader.peek();
if (event.isStartElement()) {
StartElement start = event.asStartElement();
if (start.getName().getLocalPart() == "person")) {
JAXBElement<Person> jax_b = unmarshaller.unmarshal(eventReader, Person.class);
p = jax_b.getValue();
}
}
eventReader.next();
}
} catch (Exception e) {
}
return persons;
}
I can control the amount of objects in memory using counts inside a loop (for 1000 Persons commit in database).
Upvotes: 2
Reputation: 137567
I'm guessing that the problem is you've already consumed the <person>
from the event stream so JAXB doesn't know what it is doing; it needs that element to be there so it can build the object. Thus, I suspect you need to peek the stream to decide whether to consume (and discard) or to unmarshal:
while (eventReader.hasNext()) {
XMLEvent event = eventReader.peek();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
if (startElement.getName().getLocalPart() == ("person")) {
p = (Person) unmarshaller.unmarshal(eventReader);
continue; // Assume you've done something with p; go round loop again
}
}
eventReader.nextElement(); // Discard...
}
Upvotes: 2