code: public static String escape(String string) {
          StringBuilder sb = new StringBuilder(string.length());
          for (final int cp : codePointIterator(string)) {
              switch (cp) {
              case '&':
                  sb.append("&amp;");
                  break;
              case '<':
                  sb.append("&lt;");
                  break;
              case '>':
                  sb.append("&gt;");
                  break;
              case '"':
                  sb.append("&quot;");
                  break;
              case '\'':
                  sb.append("&apos;");
                  break;
              default:
                  if (mustEscape(cp)) {
                      sb.append("&#x");
                      sb.append(Integer.toHexString(cp));
                      sb.append(';');
                  } else {
                      sb.appendCodePoint(cp);
                  }
              }
          }
          return sb.toString();
      }
docs: Replace special characters with XML escapes:
  
  <pre>
  &amp; <small>(ampersand)</small> is replaced by &amp;amp;
  &lt; <small>(less than)</small> is replaced by &amp;lt;
  &gt; <small>(greater than)</small> is replaced by &amp;gt;
  &quot; <small>(double quote)</small> is replaced by &amp;quot;
  &apos; <small>(single quote / apostrophe)</small> is replaced by &amp;apos;
  </pre>
  
  @param string
  The string to be escaped.
  @return The escaped string.