I am using JAXB to untie objects from very large XML files using XMLStreamReader.
If the XML elements that I am delimiting are separated (using a new line or even a single space), this works fine.
If the XML elements that I unmounted do not have spaces between them, I lose every other element - the XML reader seems to absorb the element after it gets unmarshalled.
A source for a simplified example that demonstrates this is at https://gist.github.com/dalelane/88df784c3cb74b214d5c
Interesting bits are:
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
JAXBContext context = JAXBContext.newInstance(MyJAXBClass.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
boolean running = true;
while (running){
switch (reader.next()){
case XMLStreamConstants.START_ELEMENT:
if (reader.getLocalName().equals("myunmarshallobjname")){
JAXBElement<MyJAXBClass> unmarshalledObj = unmarshaller.unmarshal(reader, MyJAXBClass.class);
MyJAXBClass item = unmarshalledObj.getValue();
}
break;
case XMLStreamConstants.END_DOCUMENT:
reader.close();
running = false;
break;
}
}
Every time a thread reader gets to the beginning of an element, I pass it to unmarshaller to unmount this fragment.
It works if I have XML with:
<myunmarshallobjname key="one"></myunmarshallobjname> <myunmarshallobjname key="two"></myunmarshallobjname>
, :
<myunmarshallobjname key="one"></myunmarshallobjname><myunmarshallobjname key="two"></myunmarshallobjname>
? ?