aspose file tools*
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
Author

Question on SAX parsing

chandru ram
Greenhorn

Joined: Jul 03, 2002
Posts: 18
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

Joined: Mar 22, 2000
Posts: 12821
    
    5
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

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.
Thanks
Chandru
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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]
Naren
Greenhorn

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="http://www.mqtips.com" target="_blank" rel="nofollow">MQMessenger</a> - free Java app for MQSeries
chandru ram
Greenhorn

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

Chandru
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on SAX parsing