对于中讨论的问题
XML parsing issue with '&' in element text
我有同样的问题,但是在处理之前用&替换&不能解决问题

这是我的代码:

convertedString = convertedString.replaceAll("& ", "&");
convertedString = StringEscapeUtils.unescapeHtml(convertedString);


这是我的错误:

[Fatal Error] :1:253: The entity name must immediately follow the '&' in the entity reference.
org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:249)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)


...

感谢任何人的帮助,非常感谢

最佳答案

convertedString = StringEscapeUtils.unescapeHtml(convertedString);
convertedString = convertedString.replaceAll("& ", "& "); // Also note the added space


StringEscapeUtils.unescapeHtml()实际上会将&转换回&

10-05 21:47