I’m using the following method to read in a line of text from an XML document via the web:
public static String getCharacterDataFromElement(Element e) {
Node child = ((Node) e).getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "";
}
It works fine, but if it comes across a character such as an ampersand which are not written like & etc it will then completely ignore that character and the rest of the line. What can I do to rectify this?
The only proper solution ist to correct the XML, so that the
&is written as&, or the texts are wrapped in<![CDATA[…]]>.It’s not actually XML unless you escape ampersands or use CDATA.