I am working with xml files that do not have a root element. Basically I have files that look like the following:<data>
<something></something>
</data>
<data>
<something></something>
</data>
<data>
<something></something>
</data>I've been trying to unmarshall all the <data> objects from this type of file but have not figured out how to do it. Is it possibly to unmarshall a file like this? The unmarshallDocument(reader) method successfully returns the first <data> object within the file. How can I get the rest of the <data> objects?
The solution I've come up with is to subclass Reader to patch my invalid xml files. I create a mapping for <data_list> (even though my files only contain <data> elements) and when unmarshalling I pass an instance of the following DataReader class to unmarshallDocument().
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.StringReader;public class DataReader extends Reader {
private final StringReader START_READER = new StringReader("<data_list>");
private final StringReader END_READER = new StringReader("</data_list>");
private InputStreamReader isr; public TrackingDataReader(InputStreamReader isr) {
this.isr = isr;
} public int read(char[] cbuf, int off, int len) throws IOException {
int retval = START_READER.read(cbuf, off, len);
if (retval == -1) {
retval = isr.read(cbuf, off, len);
}
if (retval == -1) {
retval = END_READER.read(cbuf, off, len);
}
return retval;
} public void close() throws IOException {
}
}