Java XML Unescape unescapeXMLString2(String str)

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

Description

unescape XML String

License

LGPL

Declaration

public static String unescapeXMLString2(String str) 

Method Source Code

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

public class Main {
    public static String unescapeXMLString2(String str) {

        StringBuffer sb = new StringBuffer();
        int index, last = 0, indexSemi;
        while ((index = str.indexOf('&', last)) != -1) {
            sb.append(str.substring(last, index));
            indexSemi = str.indexOf(';', index + 1);

            if (indexSemi == -1) {
                sb.append('&');
                last = index + 1;/*from   w w  w  . j  ava  2  s  . co  m*/
            } else if (index + 1 == indexSemi) {
                sb.append("&;");
                last = index + 2;
            } else {
                sb.append(unescapeXMLEntity(str.substring(index + 1, indexSemi)));
                last = indexSemi + 1;
            }
        }
        sb.append(str.substring(last));
        return sb.toString();
    }

    private static String unescapeXMLEntity(String str) {
        if ("lt".equals(str))
            return "<";
        if ("gt".equals(str))
            return ">";
        if ("amp".equals(str))
            return "&";
        if ("apos".equals(str))
            return "'";
        if ("quot".equals(str))
            return "\"";
        return "&" + str + ";";
    }
}

Related

  1. unescapeXmlChars(String source)
  2. unescapeXMLComment(String content)
  3. unescapeXMLEntities(String text)
  4. unescapeXMLEntity(String str)
  5. unescapeXMLString(String str)
  6. unescapeXmlSymbols(String s1)