Java XML Unescape unescapeFromXML(String string)

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

Description

unescape From XML

License

LGPL

Declaration

public static final String unescapeFromXML(String string) 

Method Source Code

//package com.java2s;
/**//  www. j  a  va  2 s.c  om
 * Converts a line of text into an array of lower case words using a
 * BreakIterator.wordInstance().
 * <p>
 * <p/>
 * This method is under the Jive Open Source Software License and was
 * written by Mark Imbriaco.
 * 
 * @param text
 *            a String of text to convert into an array of words
 * @return text broken up into an array of words.
 */

public class Main {

    public static final String unescapeFromXML(String string) {

        string = replace(string, "&lt;", "<");
        string = replace(string, "&gt;", ">");
        string = replace(string, "&quot;", "\"");
        return replace(string, "&amp;", "&");
    }

    public static final String replace(String line, String oldString, String newString) {

        if (line == null) {
            return null;
        }
        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;
    }

    public static final String replace(String line, String oldString, String newString, int[] count) {

        if (line == null) {
            return null;
        }
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            int counter = 0;
            counter++;
            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) {
                counter++;
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
                j = i;
            }
            buf.append(line2, j, line2.length - j);
            count[0] = counter;
            return buf.toString();
        }
        return line;
    }
}

Related

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