• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on SAX parsing

 
chandru ram
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an XML document in the following format :
<Response>
<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
Rancher
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Bill
 
chandru ram
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value of zero for the attribute is the required condition in my application. so IllegalArgumentException would be misleading.
Thanks
Chandru
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(...))?
 
Naren
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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')
 
chandru ram
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks all for the reply.
Iam trying out a solution based on the one posted by Naren.

Chandru
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic