2012-08-27 84 views
1

对于 XML parsing issue with '&' in element text 讨论的问题,我有同样的问题,但处理之前&更换&不能解决问题XML中元素的文本解析与“&”问题继续

这里是我的代码:

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) 

...

感激,如果有人可以帮助,非常感谢

+0

你不应该逃避'&'?你为什么试图更换它?这是不是基本上是你从前面的问题相同的问题? – MadProgrammer

+0

[XML元素文本中'&'解析问题]的可能重复(http://stackoverflow.com/questions/3838316/xml-parsing-issue-with-in-element-text) – EJP

+0

嗨,大家好,我已经更新了我上面的问题,对于混淆感到抱歉 – dale

回答

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

StringEscapeUtils.unescapeHtml()实际上转换&&

1

谢谢Arjan,抱歉,这可能是一个错误的问题。我只是意识到它是抱怨的DocumentBuilder解析器,它不接受包含&符号的xml,例如:"<text> Health & Safety </text>",但是在xml内容中有&符号是我客户端的一个要求,除非有其他方式绕过DOMparsers,将不得不操纵前端,即jsp的显示&而不是&amp;

+0

所有的html客户端显示字符串'&'为'&',所以你不需要操纵任何东西。如果你允许用户输入,你必须在保存之前修复它。 – Arjan