Java XML Unescape unescapeFromXML(String string)

Here you can find the source of unescapeFromXML(String string)

Description

Unescapes the String by converting XML escape sequences back into normal characters.

License

LGPL

Parameter

Parameter Description
string the string to unescape.

Return

the string with appropriate characters unescaped.

Declaration

public static final String unescapeFromXML(String string) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from ww w  . j  a  v a 2 s .com*/
     * Unescapes the String by converting XML escape sequences back into normal
     * characters.
     * 
     * @param string
     *            the string to unescape.
     * @return the string with appropriate characters unescaped.
     */
    public static final String unescapeFromXML(String string) {
        string = replace(string, "&lt;", "<");
        string = replace(string, "&gt;", ">");
        string = replace(string, "&quot;", "\"");
        return replace(string, "&amp;", "&");
    }

    /**
     * Replaces all instances of oldString with newString in line.
     */
    public static final String replace(String line, String oldString, String newString) {
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            char[] line2 = line.toCharArray();
            char[] newString2 = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j = i;
            while ((i = line.indexOf(oldString, i)) > 0) {
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
                j = i;
            }
            buf.append(line2, j, line2.length - j);
            return buf.toString();
        }
        return line;
    }
}

Related

  1. unescapeFromXml(String escapedString)
  2. unescapeFromXML(String st)
  3. unescapeFromXML(String string)
  4. unescapeFromXML(String string)
  5. unescapeFromXML(String string)
  6. unescapeXML(final String s)
  7. unescapeXml(String input)
  8. unescapeXml(String input)
  9. unescapeXML(String s)