I’m having Unexpected end of document on the line:
Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
I have some clues however, I think that my mistake might not be related to SAX pareser. Here is my simplified code fragment.
public boolean createDataList(InputStream inputStream) {
try {
XmlHelper xmlHelper = new XmlHelper();
ArrayList<Category> categories = xmlHelper.getCategoriesFromXml(inputStream);
ArrayList<Budget> budgetEntries = xmlHelper.getBudgetFromXml(inputStream);
ArrayList<StandingOrder> standingOrders = xmlHelper.getOrdersFromXml(inputStream);
return true;
} catch (Exception e) {
return false;
}
}
The 3 ArrayList getters are pointing to the line which is causing the problem. All of them use the same InputStream. All methods are pretty much the same, and I’m sure they work fine… just separately. Only first array getter executes successfuly, the second and third one always fails, doesnt matter what order it is, the first one goes fine.
I have suspected that there is something wrong in using same InputStream, I was trying to close the input after each array getter – no effect. Please have some clues for me. I’d really appreciate that
I suspect that while reading, the input stream advances through your file till the end. Since I don’t think the SAX parser calls mark() and reset() to position the reader to the start of the file, there is nothing left to read when you invoke it for the second time.
As a side note, why don’t you parse and create all 3 lists at the same time, and then expose them outside the XMLHelper through a getter? This should also improve your performance avoiding at least 2 full I/O operations.