jQuery in Action, 3rd edition
The moose likes XML and Related Technologies and the fly likes Problem with SAX Parser 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 "Problem with SAX Parser" Watch "Problem with SAX Parser" New topic

Problem with SAX Parser

Srikanth Raghavan
Ranch Hand

Joined: Oct 31, 2005
Posts: 389

I am parsing an XML file which is not so huge. I am using SAX parser. It doesn't fetch the data properly for exactly one element in the file.

For eg:


I am subclassing the DefaultHandler class and overriding the methods like:
characters(char[] ch, int start, int length)
endElement(String uri, String localName, String qName) and others...

While debugging though I found out that for one element it just fetches the data partially... i.e if <element2> has data data2 the characters() method just gets da and gets the remaining data (ta2) in the next iteration for the same element which is really weird.

This situation happens exactly at the same place for the same element. Are there any limitations? Or am I doing something wrong?

Incase the information is not clear or insufficient for you to help me, please let me know and I will give more information.

Thank you
Srikanth Raghavan
Ranch Hand

Joined: Oct 31, 2005
Posts: 389

I was able find out the real issue. Actually the characters() method will not return the complete data between the tags at one shot, I think it depends on some buffer size, I don't know the exact reason.

So, till we encounter the endElement() method, we have to store the data given by the characters() method in our own buffer for that current element.

Please tell me if there's a better option.

Thank you!
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
No thats the only way. SAX specifies that it does not need to return full element content in one characters() call. You need to use a string buffer and append the data from multiple character() calls until you get the endElement() call, as you said.


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem with SAX Parser
It's not a secret anymore!