I am new to parsing field. I\'m trying to write a parser code but unable to get the value with respect to a particular tag that value contains ampersand(&)
.
The problem is that the "&" is an escape character it self.
To fix this you need to replace the ampersand with a unicode equivalent, i.e: "&
"
You must replace your special characters with the characters that are accepted for an XML file. In your case & should be replaced by &
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
content = String.copyValueOf(ch, start, length).trim();
content = content.replace("&", "&")
}
You can escape these chars like html does:
<branch_name>B & P Infotech Ltd.</branch_name>
Or you have use of CDATA:
<branch_name><![CDATA[B & P Infotech Ltd.]]></branch_name>
There are some characters in XML that must not appear in their literal form in an XML document, except when used as markup delimiters or within a comment, a processing instruction, or a CDATA section.
List of characters and their corresponding entity or the numeric reference to replace :
Original Character XML entity replacement XML numeric replacement
" " "
< < <
> > >
& & &
' ' '
you must replace above character in XML before you parse it.
You may use CDATA Section for text that is not markup constitutes the character data of the document