I am trying to parse a response from a http request. This is the String I made from that response:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://my-web-address.com/webservices/"><NewDataSet>
<Case>
<ID>260023277</ID>
<CaseNumber>8931-04-03</CaseNumber>
</Case>
.
.
.
</NewDataSet></string>
All elements (represented by dots) are valid.
Here's how I'm parsing this xml using sax:
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
InputSource inputSource = new InputSource( new StringReader( responce ) ); // responce is my String containing XML.
CasesParser myXMLHandler = new CasesParser();
xr.setContentHandler(myXMLHandler);
xr.parse(inputSource);
CaseParser
class extends DefaultHandler
and implements all functions in conventional way.
So the exact error is that the parser is not detecting anything other than "string" element. It detects it in the startElement()
method of CaseParser
and then also in endElement()
method. No other element is found by the parser in between.
Upon retrieving value of "string" I get only '<'
character. Nothing else.
What might be the problem?
This is the workaround I got based on Don's Answer.
I parsed the content inside <string>
tag with XMLPullParser, stored the response in a String and then fed that String (which is now the XML without the <string>
tag)
Here's a little sample if anyone is going through the same problem:
XmlPullParserFactory factory = null;
String parsed = "";
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader (responce));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
Log.d(Constants.TAG,"Start document");
} else if(eventType == XmlPullParser.END_DOCUMENT) {
Log.d(Constants.TAG,"End document");
} else if(eventType == XmlPullParser.START_TAG) {
Log.d(Constants.TAG,"Start tag "+xpp.getName());
} else if(eventType == XmlPullParser.END_TAG) {
Log.d(Constants.TAG,"End tag "+xpp.getName());
} else if(eventType == XmlPullParser.TEXT) {
Log.d(Constants.TAG,"Text "+xpp.getText());
parsed = xpp.getText();
}
eventType = xpp.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
xmlResponse = parsed;
So from here, I feed in the xmlResponse
object to my CaseParser
object which was already written to handle and store the xml in my objects.
Hope it helps someone else too. Cheers!