private void appendEscapedString(String s, StringBuilder builder) {
        if (s == null)
            s = "";
        int pos;
        int start = 0;
        int len = s.length();
        for (pos = 0; pos < len; pos++) {
            char ch = s.charAt(pos);
            String escape;
            switch (ch) {
            case '\t':
                escape = "&#9;";
                break;
            case '\n':
                escape = "&#10;";
                break;
            case '\r':
                escape = "&#13;";
                break;
            case '&':
                escape = "&amp;";
                break;
            case '"':
                escape = "&quot;";
                break;
            case '\'' :
                escape = "&apos;";
                break;
            case '<':
                escape = "&lt;";
                break;
            case '>':
                escape = "&gt;";
                break;
            case '\u0085':
                escape = "&#133;";
                break;
            case '\u2028':
                escape = "&#8232;";
                break;
            default:
                escape = null;
                break;
            }

            // If we found an escape character, write all the characters up to that
            // character, then write the escaped char and get back to scanning
            if (escape != null) {
                if (start < pos)
                    builder.append(s, start, pos);
                sb.append(escape);
                start = pos + 1;
            }
        }

        // Write anything that's left
        if (start < pos) sb.append(s, start, pos);
    }
Appends the specified string (with any non-XML-compatible characters
replaced with the corresponding escape code) to the specified
StringBuilder.

@param s
           The string to escape and append to the specified
           StringBuilder.
@param builder
           The StringBuilder to which the escaped string should be
           appened.
aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/internal/XmlWriter.java