I’m using android’s SAX parser and I wish to stop processing after I have read N elements. Some of the feeds are quite large and can take a while to churn through. How can I stop parsing if certain conditions are met in the EndElementListener for a certain element? Here is my current listener
chanItem.setEndElementListener(new EndElementListener() {
public void end() {
_items.add(_item);
if (++_currentItem == _maxElements) {
//BREAK OUT HERE
}
}
});
I’ve tried throwing an exception within end() but EndElementListener doesn’t allow for throwing any exceptions.
Guidance would be much appreciated.
Define a new unchecked exception called “SaxBreakOutException”:
Throw this in your listener:
And catch it in the code that calls XmLReader.parse():
Alternatively, switch to Android’s XmlPullParser which doesn’t require exceptions to break out early.