Best of all is SAX or XMLPull . Android provides an API for both. The main difference is here:
- In SAX, the parser controls parsing and calls back on your code
- When parsing code, custom code controls parsing.
The following is an example parsing of XmlPull:
try {
reader = new InputStreamReader(...from soem input stream);
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(reader);
parser.require(XmlPullParser.START_DOCUMENT, null, null);
int eventType = parser.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT) {
String pName = parser.getName();
switch(eventType) {
case XmlPullParser.START_TAG:
if(pName.equals("sometag")) {
String msg = parser.nextText();
String strErrCode = parser.getAttributeValue(null, "somattr");
break;
case XmlPullParser.END_TAG:
if(pName.equals("sometag")) {
}
break;
default:
break;
}
eventType = parser.next();
}
}catch(Exception e) {
String msg = e.getMessage();
Log.e(TAG, "Error while parsing response: " + msg, e);
}
Below is a brief introduction on how to perform traction parsing.
source
share