I’m making an android application in which I’m parsing an XML using SAX parser.
In the XML there is tag:
<title>Deals & Dealmakers: Technology, media and communications M&A </title>
As you can see it contains some special charters like &
The issue is I’m using SAX’s implicit method:
@Override
public void characters(char[] ch, int start, int length) throws SAXException{}
Here, the parameter ‘char[] ch’ is supposed to fetch the entire line Deals & Dealmakers: Technology, media and communications M&A
But it is only getting “Deals “.
How can I solve this issue?
One issue might be because of the way I’m passing the XML to the SAX parser. Do I need to change the encoding or format?
Currently, I’m passing the XML as InputStream & using the below code:
HttpResponse httpResponse = utils.sendRequestAndGetHTTPResponse(URL);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
InputStream in = entity.getContent();
parseResponse(in);
}
// Inside parseResponse method:
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xmlReader = sp.getXMLReader();
MyHandler handler = new MyHandler();
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(in));
} catch (Exception e) {
}
You seem to be assuming that you’ll get the whole text in one call. There’s no guarantee of that. I strongly suspect that your
charactersmethod will be called multiple times for the same text node, which is valid for the parser to do. You need to make sure your code handles that.From the documentation:
There may be a feature you can set to ensure you get all the data in one go; I’m not sure.