Abort SAX parsing mid-document?

Posted by CSharperWithJava on Stack Overflow See other posts from Stack Overflow or by CSharperWithJava
Published on 2010-04-05T18:52:15Z Indexed on 2010/04/05 18:53 UTC
Read the original article Hit count: 391

Filed under:
|
|
|

I'm parsing a very simple XML schema with a SAX parser in Android.

An example file would be

<Lists>
<List name="foo">
<Note title="note 1" .../>
<Note title="note 2" .../>
</List>
<List name="bar">
<Note title="note 3" .../>
</List>
</Lists>

The ... represents more note data as attributes that aren't important to question.

I use a SAX parser to parse the document and only implement the startElement and 'endElement' methods of the HandlerBase to handle Note and List nodes.

However, In some cases the files can be very large and take some time to process. I'd like to be able to abort the parsing process at any time (i.e. user presses cancel button).

The best way I've come up with is to throw an exception from my startElement method when certain conditions are met (i.e. boolean stopParsing is true).

Is there a better way to do this?

I've always used DOM style parsers, so I don't fully understand the SAX parser.

One final note, I'm running this on Android, so I will have the Parser running on a worker thread to keep the UI responsive. If you know how I can kill the thread safely while the parser is running that would answer my question as well.

© Stack Overflow or respective owner

Related posts about android

Related posts about sax