public static String escapeXml(String s){
  initializeEscapeMap();
  if (s == null || s.length() == 0) {
    return s;
  }
  char[] sChars=s.toCharArray();
  StringBuilder sb=new StringBuilder();
  int lastReplacement=0;
  for (int i=0; i < sChars.length; i++) {
    if (isInvalidXMLCharacter(sChars[i])) {
      sb.append(sChars,lastReplacement,i - lastReplacement);
      sb.append(sChars[i] == 0xFFFE ? "\\ufffe" : xmlLowValueEscapeStrings[sChars[i]]);
      lastReplacement=i + 1;
    }
  }
  if (lastReplacement < sChars.length) {
    sb.append(sChars,lastReplacement,sChars.length - lastReplacement);
  }
  return StringEscapeUtils.escapeXml(sb.toString());
}

public static String escapeXml ( String s ) { initializeEscapeMap ( ) ; if ( s == null || s . length ( ) == 0 ) { return s ; } char [ ] sChars = s . toCharArray ( ) ; StringBuilder sb = new StringBuilder ( ) ; int lastReplacement = 0 ; for ( int i = 0 ; i < sChars . length ; i ++ ) { if ( isInvalidXMLCharacter ( sChars [ i ] ) ) { sb . append ( sChars , lastReplacement , i - lastReplacement ) ; sb . append ( sChars [ i ] == 0xFFFE ? "\\ufffe" : xmlLowValueEscapeStrings [ sChars [ i ] ] ) ; lastReplacement = i + 1 ; } } if ( lastReplacement < sChars . length ) { sb . append ( sChars , lastReplacement , sChars . length - lastReplacement ) ; } return StringEscapeUtils . escapeXml ( sb . toString ( ) ) ; }
escape xml entities and illegal characters in the given string . this enhances the functionality of org . apache . commons . lang . stringescapeutils . escapexml by escaping low - valued unprintable characters , which are not permitted by the w3c xml 1 . 0 specification .
