File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Question on SAX parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Question on SAX parsing" Watch "Question on SAX parsing" New topic

Question on SAX parsing

chandru ram

Joined: Jul 03, 2002
Posts: 18
I have an XML document in the following format :
<Return code="0" />
Now in the SAX parser i want to check the value of the attribute code.If the value is not zero an exception should be thrown.
In the startElement method we can check for this condition.But if the value of code attribute is zero, i have to stop the parsing. how can it be done.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
The easiest thing to do is throw an exception. I like to use IllegalArgumentException since it descends from RuntimeException and is not checked. Besides that sort of makes sense in this case.
chandru ram

Joined: Jul 03, 2002
Posts: 18
The value of zero for the attribute is the required condition in my application. so IllegalArgumentException would be misleading.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Chandru, why do you need to stop parsing? Would it be enough to set a flag and just don't do anything in the handler (return immediately from startElement(...))?

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]

Joined: Jul 21, 2003
Posts: 23
you can use this class:

And here is how I use it to stop parsing when I find the 'Element' I need. (You need to make changes to this code for 'Attribute')

<a href="" target="_blank" rel="nofollow">MQMessenger</a> - free Java app for MQSeries
chandru ram

Joined: Jul 03, 2002
Posts: 18
Thanks all for the reply.
Iam trying out a solution based on the one posted by Naren.

I agree. Here's the link:
subject: Question on SAX parsing
It's not a secret anymore!