views:

250

answers:

3

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>]]>
+2  A: 

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.

jjnguy
From a comment of Tomalak: *'And some people, when confronted with regular expressions, think "I know, I'll use a catchy quote that I remember". Now they have added nothing to the discussion.'* (I'm not implying you didn't contribute with your answer, I'm just commenting about the quote which everyone here has seen posted at least 1000000000 times by now...)
Bart Kiers
@Bart, I think it's funny.
jjnguy
@jjnguy, me too... the first 999999999 times, that is. :)
Bart Kiers
D'oh! i learned that long ago... thanks for a straightforward solution.
Mark
@Mark, no problem. Happy to help
jjnguy
+3  A: 

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

Grzegorz Oledzki
works! best answer
Mark
A: 

use an xml parser.

james