Mark
Mark

Reputation: 2038

Replace all escape sequences with non-escaped equivalent strings in java

I have a string like this:

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

I'd like to replace the escape sequences with their non-escaped characters, to end up with:

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

Upvotes: 3

Views: 1421

Answers (3)

james
james

Reputation: 468

use an xml parser.

Upvotes: -1

jjnguy
jjnguy

Reputation: 138922

Here is a non-regex solution.

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.

Upvotes: 2

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24271

StringEscapeUtils.unescapeXml() from commons-lang might be the thing you are looking for.

Upvotes: 6

Related Questions