We are fetching XML from one source and then passing onto another entity for further processing. However, the fetched XML contains special characters in the attribute value which are not acceptable to the next process. For e.g.
Sample Input :
"<Message text="<html>Welcome User, <br> Happy to have you. <br>.</html>"
Expected Output:
"<Message text="<html>Welcome User, <br> Happy to have you. <br>.</html>">
Sample Input : <Message text="<html>Welcome User, <br> Happy to have you. </html>" Multi="false"> <Meta source="system" dest="any"></Meta></Message>
Output: <Message text="<html>Welcome User, <br> Happy to have you. </html>" Multi="false"> <Meta source="system" dest="any"></Meta></Message>
But the <br>
won't be replaced in case the input has multiple <br>
tags.
We are using following code :
String xml = "<Message text=\"<html>Welcome User, <br> Happy to have you. <br>.</html>\" Multi=\"false\"><Meta source=\"system\" dest=\"any\"></Meta></Message>";
System.out.println("ORG:" + xml);
xml = replaceChars(xml);
System.out.println("NEW:" + xml);
private static String replaceChars(String xml)
{
xml = xml.replace("&", "&");
xml = xml.replaceAll("\"<([^<]*)>", "\"<$1>");
xml = xml.replaceAll("</([^<]*)>\"", "</$1>\"");
xml = xml.replaceAll("\"([^<]*)<([^<]*)>([^<]*)\"", "\"$1<$2>$3\"");
return xml;
}