Replace all escape sequences with nonequivalent equivalent strings in java

I have a line like this:

<![CDATA[<ClinicalDocument>rest of CCD here</ClinicalDocument>]]>

I would like to replace the escape sequences with their unescaped characters, so that in the end:

<![CDATA[<ClinicalDocument>rest of CCD here</ClinicalDocument>]]>
+3
source share
3 answers

Here is a solution without regular expressions.

String original = "something";

String[] escapes = new String[]{"&lt;", "&gt;"}; // add more if you need
String[] replace = new String[]{"<", ">"}; // add more if you need

String new = original;

for (int i = 0; i < escapes.length; i++) {
    new = new.replaceAll(escapes[i], replace[i]);
}

Sometimes a simple loop is easier to read, understand, and code.

+2
source

StringEscapeUtils.unescapeXml()from commons-lang may be what you are looking for.

+6
source

use the xml parser.

-1
source

Source: https://habr.com/ru/post/1749399/


All Articles