dale
dale

Reputation: 439

XML parsing issue with '&' in element text continued

For the issue discussed in XML parsing issue with '&' in element text I had the same problem, but replacing & with & before processing does not fix the problem

Here's my code:

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

Here's my error:

[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)

...

Appreciated if anyone can help, many thanks

Upvotes: 1

Views: 7838

Answers (2)

dale
dale

Reputation: 439

Thank you Arjan, and sorry guys, this is probably a wrong question asked. I just realised that it's the DocumentBuilder parser that's complaining, where it does not accept xml containing ampersand such as: "<text> Health & Safety </text>", However having ampersand inside xml content was a requirement from my client, unless there's other way to get around DOMparsers on this, I'll have to manipulate the front end, i.e. jsp's to display & instead of &amp;

Upvotes: 1

Arjan
Arjan

Reputation: 9874

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

StringEscapeUtils.unescapeHtml() would actually convert &amp; back to &.

Upvotes: 2

Related Questions