private static String handleInvalidChars(String str){
  String result="";
  final String replaceAmpresand1="&";
  final String replaceAmpresand2="amp;";
  final String replaceAmpresand=replaceAmpresand1 + replaceAmpresand2;
  final int ampLength=replaceAmpresand2.length();
  StringTokenizer strTokinizer=new StringTokenizer(str,String.valueOf(replaceAmpresand1),true);
  while (strTokinizer.hasMoreTokens()) {
    String token=strTokinizer.nextToken();
    if (token.startsWith(replaceAmpresand2)) {
      token=token.substring(ampLength);
    }
 else     if (token.equals(replaceAmpresand1)) {
      token=replaceAmpresand;
    }
    result+=token;
  }
  return result;
}

private static String handleInvalidChars ( String str ) { String result = "" ; final String replaceAmpresand1 = "&" ; final String replaceAmpresand2 = "amp;" ; final String replaceAmpresand = replaceAmpresand1 + replaceAmpresand2 ; final int ampLength = replaceAmpresand2 . length ( ) ; StringTokenizer strTokinizer = new StringTokenizer ( str , String . valueOf ( replaceAmpresand1 ) , true ) ; while ( strTokinizer . hasMoreTokens ( ) ) { String token = strTokinizer . nextToken ( ) ; if ( token . startsWith ( replaceAmpresand2 ) ) { token = token . substring ( ampLength ) ; } else if ( token . equals ( replaceAmpresand1 ) ) { token = replaceAmpresand ; } result += token ; } return result ; }
this method goes through passed string and replace each occurrence of ' & ' to ' & amp ; '
