Java XML Unescape unescapeXMLString(String str)

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

Description

unescape XML String

License

LGPL

Declaration

public static String unescapeXMLString(String str) 

Method Source Code

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

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

        StringBuffer rtn = new StringBuffer();
        int posStart = -1;
        int posFinish = -1;
        while ((posStart = str.indexOf('&', posStart)) != -1) {
            int last = posFinish + 1;

            posFinish = str.indexOf(';', posStart);
            if (posFinish == -1)
                break;
            rtn.append(str.substring(last, posStart));
            if (posStart + 1 < posFinish) {
                rtn.append(unescapeXMLEntity(str.substring(posStart + 1, posFinish)));
            } else {
                rtn.append("&;");
            }//w  w w .  j  av a 2s  . c  o  m

            posStart = posFinish + 1;
        }
        rtn.append(str.substring(posFinish + 1));
        return rtn.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. unescapeXML(String xml)
  2. unescapeXmlChars(String source)
  3. unescapeXMLComment(String content)
  4. unescapeXMLEntities(String text)
  5. unescapeXMLEntity(String str)
  6. unescapeXMLString2(String str)
  7. unescapeXmlSymbols(String s1)