Java XML Unescape unescapeXML(String str)

Here you can find the source of unescapeXML(String str)

Description

Returns a string with XML entities replaced by their normal characters.

License

Apache License

Parameter

Parameter Description
str the string to un-escape

Return

a new normal string

Declaration

public static String unescapeXML(String str) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*  w ww .  ja v  a2  s.c  om*/
     * Returns a string with XML entities replaced by their normal characters.
     *
     * @param str the string to un-escape
     * @return a new normal string
     */
    public static String unescapeXML(String str) {
        if (str == null || str.length() == 0)
            return "";

        StringBuilder buf = new StringBuilder();
        int len = str.length();
        for (int i = 0; i < len; ++i) {
            char c = str.charAt(i);
            if (c == '&') {
                int pos = str.indexOf(";", i);
                if (pos == -1) { // Really evil
                    buf.append('&');
                } else if (str.charAt(i + 1) == '#') {
                    int val = Integer.parseInt(str.substring(i + 2, pos), 16);
                    buf.append((char) val);
                    i = pos;
                } else {
                    String substr = str.substring(i, pos + 1);
                    if (substr.equals("&amp;"))
                        buf.append('&');
                    else if (substr.equals("&lt;"))
                        buf.append('<');
                    else if (substr.equals("&gt;"))
                        buf.append('>');
                    else if (substr.equals("&quot;"))
                        buf.append('"');
                    else if (substr.equals("&apos;"))
                        buf.append('\'');
                    else // ????
                        buf.append(substr);
                    i = pos;
                }
            } else {
                buf.append(c);
            }
        }
        return buf.toString();
    }
}

Related

  1. unescapeXML(final String s)
  2. unescapeXml(String input)
  3. unescapeXml(String input)
  4. unescapeXML(String s)
  5. unescapeXml(String src)
  6. unescapeXml(String str)
  7. unescapeXml(String str)
  8. unescapeXML(String text)
  9. unescapeXML(String text)