private static String escape(final String s, final char[] chr, final String[] esc) {
        StringBuilder sb = null;
        for (int i = 0; i < s.length(); i++) {
            final char c = s.charAt(i);
            int j = 0;
            while (j < chr.length) {
                if (c == chr[j]) {
                    if (sb == null) {
                        sb = new StringBuilder(s.substring(0, i));
                    }
                    sb.append(esc[j]);
                    break;
                }
                j++;
            }
            if (sb != null && j == chr.length) {
                sb.append(c);
            }
        }
        return sb == null ? s : sb.toString();
    }
Escape a literal to put into a xml document
main/src/main/java/cgeo/geocaching/brouter/util/StringUtils.java