public static String escapeXml(String s){
  if (s == null)   return null;
  StringBuilder sb=new StringBuilder();
  for (int i=0; i < s.length(); i++) {
    char c=s.charAt(i);
    if (c == '<') {
      sb.append("&lt;");
    }
 else     if (c == '>') {
      sb.append("&gt;");
    }
 else     if (c == '\'') {
      sb.append("&apos;");
    }
 else     if (c == '&') {
      sb.append("&amp;");
    }
 else     if (c == '"') {
      sb.append("&quot;");
    }
 else {
      sb.append(c);
    }
  }
  return sb.toString();
}

public static String escapeXml ( String s ) { if ( s == null ) return null ; StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char c = s . charAt ( i ) ; if ( c == '<' ) { sb . append ( "&lt;" ) ; } else if ( c == '>' ) { sb . append ( "&gt;" ) ; } else if ( c == '\'' ) { sb . append ( "&apos;" ) ; } else if ( c == '&' ) { sb . append ( "&amp;" ) ; } else if ( c == '"' ) { sb . append ( "&quot;" ) ; } else { sb . append ( c ) ; } } return sb . toString ( ) ; }
escape the 5 entities defined by xml .
