Java XML Unescape unescapeFromXml(String escapedString)

Here you can find the source of unescapeFromXml(String escapedString)

Description

Un-escape a string from its XML encoding - in particular, un-escape &, <, >, ", and hex encoded characters.

License

Open Source License

Parameter

Parameter Description
escapedString the string to un-escape

Return

the un-escaped string

Declaration

public static String unescapeFromXml(String escapedString) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*  ww w  . j  a va2  s .  c  o m*/
     * Un-escape a string from its XML encoding - in particular, un-escape
     * &amp;, &lt;, &gt;, &quot;, and hex encoded characters.
     * @param escapedString the string to un-escape
     * @return the un-escaped string
     */
    public static String unescapeFromXml(String escapedString) {
        StringBuffer string = new StringBuffer();
        for (int c = 0; c < escapedString.length();) {
            char ch = escapedString.charAt(c);
            if (ch == '&') {
                if (escapedString.startsWith("amp;", (c + 1))) {
                    string.append('&');
                    c += 5;
                } else if (escapedString.startsWith("lt;", (c + 1))) {
                    string.append('<');
                    c += 4;
                } else if (escapedString.startsWith("gt;", (c + 1))) {
                    string.append('>');
                    c += 4;
                } else if (escapedString.startsWith("quot;", (c + 1))) {
                    string.append('"');
                    c += 6;
                } else if (escapedString.startsWith("apos;", (c + 1))) {
                    string.append('\'');
                    c += 6;
                } else if (escapedString.startsWith("#x", (c + 1))) {
                    int sci = escapedString.indexOf(';', (c + 2));
                    if ((sci != -1) && (sci <= (c + 4)))
                        try {
                            ch = ((char) Integer.parseInt(escapedString.substring((c + 3), sci), 16));
                            c = sci;
                        } catch (Exception e) {
                        }
                    string.append(ch);
                    c++;
                } else if (escapedString.startsWith("#", (c + 1))) {
                    int sci = escapedString.indexOf(';', (c + 1));
                    if ((sci != -1) && (sci <= (c + 4)))
                        try {
                            ch = ((char) Integer.parseInt(escapedString.substring((c + 2), sci)));
                            c = sci;
                        } catch (Exception e) {
                        }
                    string.append(ch);
                    c++;
                } else if (escapedString.startsWith("x", (c + 1))) {
                    int sci = escapedString.indexOf(';', (c + 1));
                    if ((sci != -1) && (sci <= (c + 4)))
                        try {
                            ch = ((char) Integer.parseInt(escapedString.substring((c + 2), sci), 16));
                            c = sci;
                        } catch (Exception e) {
                        }
                    string.append(ch);
                    c++;
                } else {
                    string.append(ch);
                    c++;
                }
            } else {
                string.append(ch);
                c++;
            }
        }
        return string.toString();
    }
}

Related

  1. unescapedKeyNameXml(final String name)
  2. unescapeForXML(String string)
  3. unescapeFromXML(String st)
  4. unescapeFromXML(String string)
  5. unescapeFromXML(String string)
  6. unescapeFromXML(String string)