I am trying to parse an XML file whcih contains some special characters like \"&\" using DOM parser. I am getting the saxparse exception \"the reference to entity must e
Building on an answer above from PSpeed the following replaceAll regex and replacement text will replace all unescaped ampersands with escaped ampersands.
String clean = xml.replaceAll( ("(&(?!amp;))", "&") );
The pattern is a negative lookahead to match on any ampersands that have not yet been escaped and the replacement string is simply an escaped ampersand. This can be optimized further for performance by using a statically compiled Pattern.
private final static Pattern unescapedAmpersands = Pattern.compile("(&(?!amp;))");
...
Matcher m = unescapedAmpersands.matcher(xml);
String xmlWithAmpersandsEscaped = m.replaceAll("&");