I am trying to port a function I wrote in ActionScript to Java and I am having a bit of trouble. I have included the function below. I found this response to question #375420, but do I really need to write a separate class? Thanks.
public static function replaceXML(str:String):String {
return str.replace(/[\"'&<>]/g, function($0:String):String {
return StringUtil.substitute('&#{0};', $0.charCodeAt(0));
});
}
Input
<root><child id="foo">Bar</child></root>
Output
<root><child id="foo">Bar</child></root>
UPDATE
Here is my solution if anyone is wondering. Thanks Sri Harsha Chilakapati.
public static String replaceXML(final String inputStr) {
String outputStr = inputStr;
Matcher m = Pattern.compile("[&<>'\"]").matcher(outputStr);
String found = "";
while (m.find()) {
found = m.group();
outputStr = outputStr.replaceAll(found,
String.format("&#%d;", (int)found.charAt(0)));
}
return outputStr;
}
You can use regex for that.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String myString = "<root><child id=\"foo\">Bar</child></root>";
Matcher m = Pattern.compile("[^\\p{L}\\p{N};\"+*/-]").matcher(myString);
while (m.find()) {
String found = m.group();
myString = myString.replaceAll(found, "&#" + (int)found.charAt(0) + ";");
}
System.out.println(myString);
It's working.
Output is
<root><child id="foo">Bar</child>&60;/root>