public static String removeInvalidCharacterDataChars(String s){
  boolean hasInvalidChars=false;
  int length=s.length();
  for (int i=0; i < length; i++) {
    if (!isValidCharacterDataChar(s.charAt(i))) {
      hasInvalidChars=true;
      break;
    }
  }
  if (hasInvalidChars) {
    StringBuilder buffer=new StringBuilder(length);
    for (int i=0; i < length; i++) {
      char c=s.charAt(i);
      if (isValidCharacterDataChar(c)) {
        buffer.append(c);
      }
    }
    return buffer.toString();
  }
 else {
    return s;
  }
}

public static String removeInvalidCharacterDataChars ( String s ) { boolean hasInvalidChars = false ; int length = s . length ( ) ; for ( int i = 0 ; i < length ; i ++ ) { if ( ! isValidCharacterDataChar ( s . charAt ( i ) ) ) { hasInvalidChars = true ; break ; } } if ( hasInvalidChars ) { StringBuilder buffer = new StringBuilder ( length ) ; for ( int i = 0 ; i < length ; i ++ ) { char c = s . charAt ( i ) ; if ( isValidCharacterDataChar ( c ) ) { buffer . append ( c ) ; } } return buffer . toString ( ) ; } else { return s ; } }
removes all non - valid xml character data chars from the specified string . if all characters are valid character data chars , this method will return the same string .
