Java XML Unescape unescapeXML(String text)

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

Description

unescape a String in XML.

License

Open Source License

Declaration

public static String unescapeXML(String text) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** xml patterns that will be unescaped. Have to be the longest first */
    private static String[] to_unescape = { " ", "—", "–", "&", """, "'",
            "&#039", "—", "&ndash", " " };
    private static char[] unescaped = { '\u0020', '\u2014', '\u2013', '&', '"', '\'', '\'', '\u2014', '\u2013',
            '\u0020' };

    /** unescape a String in XML. . */
    public static String unescapeXML(String text) {
        if (text.contains("&")) {
            StringBuilder result = new StringBuilder(text.length());
            int n = text.length();
            for (int i = 0; i < n; i++) {
                char c = text.charAt(i);
                if (c == '&') {
                    for (int unescaped_index = 0; unescaped_index < to_unescape.length; unescaped_index++) {
                        String pattern = to_unescape[unescaped_index];
                        boolean match = true;
                        if (n <= i + pattern.length())
                            continue;
                        for (int i_sub = 0; i_sub < pattern.length(); i_sub++) {
                            if (text.charAt(i + i_sub) != pattern.charAt(i_sub)) {
                                match = false;
                                break;
                            }/* ww  w  .j av  a  2s  .c  om*/
                        }
                        if (match) {
                            result.append(unescaped[unescaped_index]);
                            i += pattern.length() - 1;
                            break;
                        }
                    }
                } else {
                    result.append(c);
                }
            }
            return result.toString().trim();
        } else
            return text.trim();
    }
}

Related

  1. unescapeXml(String src)
  2. unescapeXml(String str)
  3. unescapeXML(String str)
  4. unescapeXml(String str)
  5. unescapeXML(String text)
  6. unescapeXml(String value)
  7. unescapeXml(String value)
  8. unescapeXml(String xml)
  9. unescapeXML(String xml)