0

Unable to unescape consecutive escape char like <errors>. Below is the sample code:

    String error = "<errors>";
    String out = org.apache.commons.lang3.StringEscapeUtils.unescapeXml(error);
    System.out.println(out);

Current Output: <errors>

Expected output: <errors>

This code ran on JDK 11.0.16 version and commons-lang3-3.4.jar version.

ompratap
  • 9
  • 1
  • 5
  • If you expect ```&```Your error string is malformned, ```lt;``` and ```gt;``` aren't html entities. – Thilo Schwarz Mar 27 '23 at 09:14
  • My expectation is as output. – ompratap Mar 27 '23 at 09:20
  • correct encode string: ```<errors>```. ```&``` doesn't matter in this context. – Thilo Schwarz Mar 27 '23 at 09:32
  • 1
    `&` is escape for `&`, so ..you expect "double unescape": `&lt;errors&gt;` -> `<errors>` -> `` – xerx593 Mar 27 '23 at 09:56
  • Agree to run many times. But I Suprised was in one of my environments this is working. There ran on JDK1.8. However, it's not working as a standalone application JDk (1.8 and JDK11) as well. something is weared. Though JDK upgrade issue but isn't. – ompratap Mar 27 '23 at 10:59

1 Answers1

0

You need to unescape the String as many times as the input String has been escaped.

var input = "&amp;lt;errors&amp;gt;";
var output = StringEscapeUtils.unescapeXml(input);
output = StringEscapeUtils.unescapeXml(output);
System.out.println(output);
Bombe
  • 81,643
  • 20
  • 123
  • 127