codecompleting
codecompleting

Reputation: 9621

With SAX, is it possible to exit from processing?

I want to read a specific node in an xml document, and I want to do it the fastest way possible and I believe SAX is the answer.

I'm using xerces right now, and I understand that the actual parsing is done using events.

So I have setup my start/end element events etc., and I want to know if it is possible to exit processing after reading a specific element?

i.e. once I get to a specific xml element, and read the contents, if it meets some criteria, I want to stop processing the xml.

Upvotes: 1

Views: 146

Answers (2)

勿绮语
勿绮语

Reputation: 9330

After you got what you want, simply:

throw new SAXException("I got what I want!");

Upvotes: 4

javanna
javanna

Reputation: 60225

if you think throwing an exception with Sax is a bit hacky (I wouldn't like that as well), you should consider to use Stax instead of Sax. Tha main difference is that Sax uses a push approach, while with Stax it's your code that pulls events, so you have definitely more control and you can exit whenever you want.

I don't think there are such big performance differences between SAX and Stax. I completely switched to Stax because of the more control I have on the execution flow, which even means more readable code. Just give it a try!

Upvotes: 2

Related Questions