I am trying to get xml from string. Specific symbols locate in tags title. I did it:
public class Demo {
public static void main(String[] args) throws Exception {
String data = "<title> \"sad\" <<dd> ><\n </title>";
String pattern = "(<title>)(.+?)([<>'\"&])(.+?)(\n </title>)";
Matcher m = Pattern.compile(pattern).matcher(data);
while (m.find()) {
String bugString = m.group(3) + m.group(4);
String fixed = bugString.replaceAll("<", "<");
fixed = fixed.replaceAll(">", ">");
fixed = fixed.replaceAll(">", ">");
fixed = fixed.replaceAll("'", "'");
fixed = fixed.replaceAll("\"", """);
fixed = fixed.replaceAll("&", "&");
data = data.replace(bugString, fixed);
}
System.out.println(data);
}
}
But it looks a little ugly. How I can improve it, if I don't want use additional library?