When we extend DefaultHandler, we usually override the characters function.
I was wondering which would be a more efficient way to extract the String supplied...
Would it be using a for loop and a StringBuilder?
@Override public void characters(char[] ch, int start, int length) throws SAXException { StringBuilder sb = new StringBuilder(length); for(int i=start; i<start+length; i++) sb.append(ch[i]); String values = sb.toString(); }Would it be using a simple substring?
@Override public void characters(char[] ch, int start, int length) throws SAXException { String values = (new String(ch)).substring(start, start + length); }Is there another approach?
#characters(char[] ch, int, int)method may be called several times in a row and you should append character data in aStringBuilderclass member and not process it until you hit the relevant#endElement(String,String,String)call. Appending chunks of characters should be done by callingStringBuilder#append(char[], int, int).ContentHandlers manually you should probably consider using some sort of framework - even a simple one like the built in android.sax would probably simply things for you.