Need help figuring out why my stringbuffer is empty after updating it in a overwritten SAX callback method. My code:
public class SAXXMLParser extends DefaultHandler
{
SAXParserFactory factory;
SAXXMLParser dh;
SAXParser sax;
StringBuffer accumulator;
String remember = "";
public String saxParse( String msgToParse, String uriPathExpression )
throws SAXException, IOException, ParserConfigurationException
{
System.out.println( "saxParse" );
factory = SAXParserFactory.newInstance();
sax = factory.newSAXParser();
dh = new SAXXMLParser();
InputStream is = new ByteArrayInputStream( msgToParse.getBytes() );
sax.parse( is , dh );
System.err.println( remember );
return remember;
}
public void startElement( String uri, String localName, String elementName, Attributes attrs )
{
accumulator.setLength(0);
}
public void endElement( String uri, String name, String qName )
{
System.out.println( qName );
if( qName.equalsIgnoreCase( "my element" ) )
{
remember = accumulator.toString();
}
}
public void characters( char[] ch, int start, int len )
{
accumulator.append( ch, start, len );
}
public void startDocument()
{
accumulator = new StringBuffer();
}
}
In another class, I call SAXXMLParser.saxParse( string, string ) expecting a string to be returned (remember) but remember is always null.
You need to return dh.remember
instead of remember
or
call sax.parse( is , this );
instead of sax.parse( is , dh );