Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SAX parsing

 
Sandeep Lakshmipathy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,
I am new to XML. When I use the Sax parser to parse an xml file, the control comes to the characters(..) method more than once for a single element value which i happen to store in a String.
To avoid multiple entries to this method, I am using a boolean value to this. Is this a natural behaviour of the parser, or i am going wrong some where . Thanks in advance
Sandeep
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. It is the natural behaviour of the SAXParser. The character method may be called multiple number of times.
The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity.
The application must not attempt to read from the array outside of the specified range indicated by the start index and end index parameters.

Hope that helps.
 
Sandeep Lakshmipathy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ajith.
This means its better I use the StringBuffer instead of String variable to store the char chunks as they arrive for a single element's value. By this, I can discard the use of the boolean variable that I now presume is an incorrect implementation since the same value could come in one or more chunks and I am expected to wait and append all those chunks to get the full value for the element. Please correct me if wrong.
Thanks a lot.
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are correct. You can simply read the characters from argument array within the start to end range and keep appending them to a StringBuffer. That will work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic