public static String escapeSpecialCharacters(String txt){
  if (txt == null) {
    return txt;
  }
  int len=txt.length();
  if (len == 0) {
    return txt;
  }
  StringBuilder buf=new StringBuilder();
  for (int i=0; i < len; i++) {
    char c=txt.charAt(i);
switch (c) {
case '&':
      buf.append("&amp;");
    break;
case '<':
  buf.append("&lt;");
break;
case '>':
buf.append("&gt;");
break;
case '\"':
buf.append("&quot;");
break;
case '\'':
buf.append("&apos;");
break;
case '/':
buf.append("&#47;");
break;
case '\u00A0':
buf.append("&#x00A0;");
break;
default :
buf.append(c);
}
}
return buf.toString();
}

public static String escapeSpecialCharacters ( String txt ) { if ( txt == null ) { return txt ; } int len = txt . length ( ) ; if ( len == 0 ) { return txt ; } StringBuilder buf = new StringBuilder ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = txt . charAt ( i ) ; switch ( c ) { case '&' : buf . append ( "&amp;" ) ; break ; case '<' : buf . append ( "&lt;" ) ; break ; case '>' : buf . append ( "&gt;" ) ; break ; case '\"' : buf . append ( "&quot;" ) ; break ; case '\'' : buf . append ( "&apos;" ) ; break ; case '/' : buf . append ( "&#47;" ) ; break ; case ' ' : buf . append ( "&#x00A0;" ) ; break ; default : buf . append ( c ) ; } } return buf . toString ( ) ; }
the method escapes ' & ' , ' < ' , ' > ' , ' " ' , ' ' '
