org.xml.sax.SAXParseException: The reference to entity “T” must end with the ';' delimiter

前端 未结 9 1675
北海茫月
北海茫月 2020-12-29 07:35

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

9条回答
  •  鱼传尺愫
    2020-12-29 07:57

    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("&");
    

提交回复
热议问题