According to the SAX, Default Handler documentation,
public void characters(char[] ch,
int start,
int length)
throws SAXException
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
so that the Locator provides useful information.
So the parser may call the characters method one or multiple times for a particular text inside an element say, "Don't forget me this weekend!", until the whole text is read.
Note:
The application must not attempt to read from the array outside of the
specified range.
The below code shows how to collect the text inside a single XML Element.
boolean isTagInScope = false;
StringBuilder elementContent = new StringBuilder();
public void startElement(String namespaceURI, String lName, String qName,
Attributes attributes) throws SAXException
{
isTagInScope = true;
}
public void endElement(String namespaceURI, String sName, String qName)
throws SAXException throws SAXException {
isTagInScope = false;
}
public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
if(isTagInScope)
{
String content = new String(arg0, arg1, arg2);
elementContent.append(content);
}
}
The 'elementContent' variable will hold the entire content between start and end tags of an element.