Java Utililty Methods XML Unescape

List of utility methods to do XML Unescape

Description

The list of methods to do XML Unescape are organized into topic(s).

Method

StringunescapeXml(String xml)
unescape Xml
if (xml == null)
    return null;
StringBuilder b = new StringBuilder();
int i = 0;
while (i < xml.length()) {
    if (xml.charAt(i) == '&') {
        StringBuilder e = new StringBuilder();
        i++;
...
StringunescapeXML(String xml)
unescape XML
return xml.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&");
StringunescapeXmlChars(String source)
Unescape XML special characters (<, >, & and ")
return source.replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&").replaceAll("&quot;",
        "\"");
StringunescapeXMLComment(String content)
XML comment does not support some characters inside its content but there is no official escaping/unescaping for it so we made our own.
StringBuffer str = new StringBuffer(content.length());
char[] buff = content.toCharArray();
boolean escaped = false;
for (char c : buff) {
    if (!escaped && c == '\\') {
        escaped = true;
        continue;
    str.append(c);
    escaped = false;
return str.toString();
StringunescapeXMLEntities(String text)
Converts XML entities to characters.
if (text.contains("&gt;")) {
    text = text.replaceAll("&gt;", ">");
if (text.contains("&lt;")) {
    text = text.replaceAll("&lt;", "<");
if (text.contains("&quot;")) {
    text = text.replaceAll("&quot;", "\"");
...
StringunescapeXMLEntity(String str)
unescape XML Entity
if ("lt".equals(str))
    return "<";
if ("gt".equals(str))
    return ">";
if ("amp".equals(str))
    return "&";
if ("apos".equals(str))
    return "'";
...
StringunescapeXMLString(String str)
unescape XML String
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;
...
StringunescapeXMLString2(String str)
unescape XML String
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;
...
StringunescapeXmlSymbols(String s1)
unescape Xml Symbols
String s2 = s1.replaceAll("&lt;", "<");
s2 = s2.replaceAll("&gt;", ">");
s2 = s2.replaceAll("&gt;", ">");
s2 = s2.replaceAll("&#xD;", "\r");
s2 = s2.replaceAll("&apos;", "'");
s2 = s2.replaceAll("&quot;", "\"");
s2 = s2.replaceAll("&amp;", "&");
s2 = s2.replaceAll("&#xA;", "\n");
...