Hi everybody,
How does the character method of ContentHandler work ?
I am parsing a simple xml document using the SAX parser :
<?xml version='1.0' encoding='utf-8'?>
<bookstore>
<book>
<title> THE GREATNESS GUIDE </title>
<author> Robin Sharma</author>
</book>
<book>
<title>ONE NIGHT AT THE CALL CENTER </title>
<author>Shyam Bhagwat </author>
</book>
</bookstore>
public class MySAXHandler2 extends DefaultHandler
{
public void startElement(
String uri ,String localname ,String qName ,Attributes attr) throws SAXException
{
System.out.println("<" + qName + ">") ;
}
public void endElement(String uri ,String localName ,String qName) throws SAXException
{
System.out.println("</" + qName + ">") ;
}
public void characters(char [] ch ,int start ,int length) throws SAXException
{
System.out.println(" *" + new String(ch) ) ;//new String(ch,start,length) ) ; line *****
}
}
The output for this is :
---------- interpreter ----------
<bookstore>
*
<bookstore>
<book>
<title> THE GREATNESS GUIDE </title>
<author> Robin Sharma</author>
</book>
<book>
<title>ONE NIGHT AT THE CALL CENTER </title>
<author>Shyam Bhagwat </author>
</book>
</bookstore>
Output completed (0 sec consumed)
Now if I simply replace new String(ch) in line ***** with new String(ch,start,length) I get a proper output :
---------- interpreter ----------
<bookstore>
*
<book>
*
<title>
* THE GREATNESS GUIDE
</title>
*
<author>
* Robin Sharma
</author>
*
</book>
*
<book>
*
<title>
*ONE NIGHT AT THE CALL CENTER
</title>
*
<author>
*Shyam Bhagwat
</author>
*
</book>
*
</bookstore>
Output completed (0 sec consumed) - Normal Termination
Why So??
Cheers,
Poonam.