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

    int idxS = s.indexOf('&');
    if(idxS < 0)
      return s;

    StringBuilder sb = new StringBuilder(s.length());

    int idxE, idx, size;
    char c, c_1;
    int prev = 0;

    while(idxS != -1)
    {
      idxE = s.indexOf(';', idxS);
      if(idxE < 0)
        throw new IllegalArgumentException("missing ';' in: " +
                                           s.substring(idxS));

      sb.append(s.substring(prev, idxS));

      idx = idxS + 1;
      size = idxE - idxS - 1;
      if(size < 2)
        throw new IllegalArgumentException("invalid escape tag: " +
                                           s.substring(idxS, idxE + 1));
      c = s.charAt(idx);
      c_1 = s.charAt(idx + 1);
      switch(c)
      {
        case 'l':
          if(size != 2)
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          if(c_1 == 't')
            sb.append('<');
          else
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          break;

        case 'g':
          if(size != 2)
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          if(c_1 == 't')
            sb.append('>');
          else
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          break;

        case 'q':
          if(size != 4)
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          if(c_1 != 'u' || s.charAt(idx + 2) != 'o' ||
             s.charAt(idx + 3) != 't')
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          else
            sb.append('"');
          break;

        case 'a':
          if(size == 3)
          {
            if(c_1 != 'm' || s.charAt(idx + 2) != 'p')
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
            else
              sb.append('&');
          }
          else if(size == 4)
          {
            if(c_1 != 'p' || s.charAt(idx + 2) != 'o' ||
               s.charAt(idx + 3) != 's')
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
            else
              sb.append('\'');
          }
          else
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          break;

        case '#':
          int codePoint;
          try
          {
            codePoint = (c_1 == 'x')
              ? Integer.parseInt(s.substring(idx + 2, idxE), 16)
              : Integer.parseInt(s.substring(idx + 1, idxE));
          }
          catch(NumberFormatException ex)
          {
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          }

          if (codePoint < 0)
            throw new IllegalArgumentException("invalid character codepoint: " +
                                               s.substring(idxS, idxE + 1));

          if (codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT)
            sb.append((char) codePoint);
          else
            sb.append(Character.toChars(codePoint));
          break;

        default:
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
      }
      prev = idxE + 1;
      idxS = s.indexOf("&", prev);
    }
    if(prev < s.length())
      sb.append(s.substring(prev));

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

    int idxS = s.indexOf('&');
    if(idxS < 0)
      return s;

    StringBuilder sb = new StringBuilder(s.length());

    int idxE, idx, size;
    char c, c_1;
    int prev = 0;

    while(idxS != -1)
    {
      idxE = s.indexOf(';', idxS);
      if(idxE < 0)
        throw new IllegalArgumentException("missing ';' in: " +
                                           s.substring(idxS));

      sb.append(s.substring(prev, idxS));

      idx = idxS + 1;
      size = idxE - idxS - 1;
      if(size < 2)
        throw new IllegalArgumentException("invalid escape tag: " +
                                           s.substring(idxS, idxE + 1));
      c = s.charAt(idx);
      c_1 = s.charAt(idx + 1);
      switch(c)
      {
        case 'l':
          if(size != 2)
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          if(c_1 == 't')
            sb.append('<');
          else
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          break;

        case 'g':
          if(size != 2)
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          if(c_1 == 't')
            sb.append('>');
          else
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          break;

        case 'q':
          if(size != 4)
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          if(c_1 != 'u' || s.charAt(idx + 2) != 'o' ||
             s.charAt(idx + 3) != 't')
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          else
            sb.append('"');
          break;

        case 'a':
          if(size == 3)
          {
            if(c_1 != 'm' || s.charAt(idx + 2) != 'p')
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
            else
              sb.append('&');
          }
          else if(size == 4)
          {
            if(c_1 != 'p' || s.charAt(idx + 2) != 'o' ||
               s.charAt(idx + 3) != 's')
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
            else
              sb.append('\'');
          }
          else
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          break;

        case '#':
          int codePoint;
          try
          {
            codePoint = (c_1 == 'x')
              ? Integer.parseInt(s.substring(idx + 2, idxE), 16)
              : Integer.parseInt(s.substring(idx + 1, idxE));
          }
          catch(NumberFormatException ex)
          {
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
          }

          if (codePoint < 0)
            throw new IllegalArgumentException("invalid character codepoint: " +
                                               s.substring(idxS, idxE + 1));

          if (codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT)
            sb.append((char) codePoint);
          else
            sb.append(Character.toChars(codePoint));
          break;

        default:
            throw new IllegalArgumentException("invalid escape tag: " +
                                               s.substring(idxS, idxE + 1));
      }
      prev = idxE + 1;
      idxS = s.indexOf("&", prev);
    }
    if(prev < s.length())
      sb.append(s.substring(prev));

    return sb.toString();
  }
This method descodes a string that was previously encoded for being XML
safe. It is the exact opposite of xmlEncode

@param s the string to decode
@return the decoded string
@exception IllegalArgumentException if the string cannot be decoded