org.linkedin.util-core/src/main/java/org/linkedin/util/xml/XMLUtils.java
public static String xmlEncodeRaw(String s)
  {
    if(s == null)
      return null;

    char[] chars = s.toCharArray();
    final int len = chars.length;
    StringBuilder sb = new StringBuilder(len);

    for(int i = 0; i < len; i++)
    {
      char c = chars[i];
      if(c < _chars.length)
      {
        sb.append(_chars[c]);
      }
      else if (Character.isHighSurrogate(c))
      {
        int j = i+1;
        if (j < len && Character.isLowSurrogate(chars[j]))
        {
          sb.append("&#x").append(Integer.toHexString(Character.toCodePoint(c, chars[j]))).append(';');
          i = j;
        }
        // else ignore this character, a single high surrogate is an invalid char
      }
      else if (!Character.isLowSurrogate(c))
      {
        sb.append("&#x").append(Integer.toHexString(c)).append(';');
      }
    }

    return sb.toString();
  }
public static String xmlEncodeRaw(String s)
  {
    if(s == null)
      return null;

    char[] chars = s.toCharArray();
    final int len = chars.length;
    StringBuilder sb = new StringBuilder(len);

    for(int i = 0; i < len; i++)
    {
      char c = chars[i];
      if(c < _chars.length)
      {
        sb.append(_chars[c]);
      }
      else if (Character.isHighSurrogate(c))
      {
        int j = i+1;
        if (j < len && Character.isLowSurrogate(chars[j]))
        {
          sb.append("&#x").append(Integer.toHexString(Character.toCodePoint(c, chars[j]))).append(';');
          i = j;
        }
        // else ignore this character, a single high surrogate is an invalid char
      }
      else if (!Character.isLowSurrogate(c))
      {
        sb.append("&#x").append(Integer.toHexString(c)).append(';');
      }
    }

    return sb.toString();
  }
Encodes the string so that it is XML safe: all characters &lt;= 127
will not be encoded (converts &lt;, &gt; &amp; &apos; into their
equivalent (&amp;lt;, &amp;gt; &amp;amp; &amp;apos;)). All characters
&gt; 127 will be encoded as &amp;#xxxx; with xxxx the hexadecimal
value of the character. This method differs from
<code>xmlEncode</code> because it encodes all values &gt; 127 into a
pure ascii xml safe string. In most cases, this method is NOT
necessary because with an xml message you can specify the encoding, so
there is no need to encode those characters.

@param s the string to encode
@return the encoded string