src/main/java/org/jivesoftware/whack/util/StringUtils.java
public static final String escapeForXML(String string) {
        if (string == null) {
            return null;
        }
        char ch;
        int i = 0;
        int last = 0;
        char[] input = string.toCharArray();
        int len = input.length;
        StringBuffer out = new StringBuffer((int) (len * 1.3));
        for (; i < len; i++) {
            ch = input[i];
            if (ch > '>') {
                continue;
            } else if (ch == '<') {
                if (i > last) {
                    out.append(input, last, i - last);
                }
                last = i + 1;
                out.append(LT_ENCODE);
            } else if (ch == '>') {
                if (i > last) {
                    out.append(input, last, i - last);
                }
                last = i + 1;
                out.append(GT_ENCODE);
            }

            else if (ch == '&') {
                if (i > last) {
                    out.append(input, last, i - last);
                }
                // Do nothing if the string is of the form &#235; (unicode
                // value)
                if (!(len > i + 5 && input[i + 1] == '#'
                        && Character.isDigit(input[i + 2])
                        && Character.isDigit(input[i + 3])
                        && Character.isDigit(input[i + 4]) && input[i + 5] == ';')) {
                    last = i + 1;
                    out.append(AMP_ENCODE);
                }
            } else if (ch == '"') {
                if (i > last) {
                    out.append(input, last, i - last);
                }
                last = i + 1;
                out.append(QUOTE_ENCODE);
            }
        }
        if (last == 0) {
            return string;
        }
        if (i > last) {
            out.append(input, last, i - last);
        }
        return out.toString();
    }
public static final String escapeForXML(String string) {
        if (string == null) {
            return null;
        }
        char ch;
        int i = 0;
        int last = 0;
        char[] input = string.toCharArray();
        int len = input.length;
        StringBuffer out = new StringBuffer((int) (len * 1.3));
        for (; i < len; i++) {
            ch = input[i];
            if (ch > '>') {
                continue;
            } else if (ch == '<') {
                if (i > last) {
                    out.append(input, last, i - last);
                }
                last = i + 1;
                out.append(LT_ENCODE);
            } else if (ch == '>') {
                if (i > last) {
                    out.append(input, last, i - last);
                }
                last = i + 1;
                out.append(GT_ENCODE);
            }

            else if (ch == '&') {
                if (i > last) {
                    out.append(input, last, i - last);
                }
                // Do nothing if the string is of the form &#235; (unicode
                // value)
                if (!(len > i + 5 && input[i + 1] == '#'
                        && Character.isDigit(input[i + 2])
                        && Character.isDigit(input[i + 3])
                        && Character.isDigit(input[i + 4]) && input[i + 5] == ';')) {
                    last = i + 1;
                    out.append(AMP_ENCODE);
                }
            } else if (ch == '"') {
                if (i > last) {
                    out.append(input, last, i - last);
                }
                last = i + 1;
                out.append(QUOTE_ENCODE);
            }
        }
        if (last == 0) {
            return string;
        }
        if (i > last) {
            out.append(input, last, i - last);
        }
        return out.toString();
    }
public static final String escapeForXML ( String string ) { if ( string == null ) { return null ; } char ch ; int i = 0 ; int last = 0 ; char [ ] input = string . toCharArray ( ) ; int len = input . length ; StringBuffer out = new StringBuffer ( ( int ) ( len * 1.3 ) ) ; for ( ; i < len ; i ++ ) { ch = input [ i ] ; if ( ch > ' ' ) { continue ; } else if ( ch == ' ' ) { if ( i > last ) { out . append ( input , last , i - last ) ; } last = i + 1 ; out . append ( LT_ENCODE ) ; } else if ( ch == ' ' ) { if ( i > last ) { out . append ( input , last , i - last ) ; } last = i + 1 ; out . append ( GT_ENCODE ) ; } else if ( ch == ' ' ) { if ( i > last ) { out . append ( input , last , i - last ) ; } // Do nothing if the string is of the form &#235; (unicode // value) if ( ! ( len > i + 5 && input [ i + 1 ] == ' ' && Character . isDigit ( input [ i + 2 ] ) && Character . isDigit ( input [ i + 3 ] ) && Character . isDigit ( input [ i + 4 ] ) && input [ i + 5 ] == ' ' ) ) { last = i + 1 ; out . append ( AMP_ENCODE ) ; } } else if ( ch == ' ' ) { if ( i > last ) { out . append ( input , last , i - last ) ; } last = i + 1 ; out . append ( QUOTE_ENCODE ) ; } } if ( last == 0 ) { return string ; } if ( i > last ) { out . append ( input , last , i - last ) ; } return out . toString ( ) ; }
Escapes all necessary characters in the String so that it can be used in
an XML doc.

@param string
the string to escape.
@return the string with appropriate characters escaped.
Escapes all necessary characters in the String so that it can be used in an XML doc .