I'm building an Android application which parses xml data from an API. For the one fragment, everything works well, as I only need to get one set of data and present it on screen. However, for the other fragment, it needs to pull multiple sets back but the code does not seem to work for this. It most likely just needs a tweak somewhere. This is the xml I get back:
<api-response>
<response-begin/>
<method>blablabla</method>
<result>
<authentication-count>2</authentication-count>
<authentication>
<connection-time>10 September 2014 @ 12:29</connection-time>
<ip-address>*******</ip-address>
<nas-ip-address>*******</nas-ip-address>
<nas-port>*******</nas-port>
</authentication>
<authentication>
<connection-time>09 September 2014 @ 17:09</connection-time>
<ip-address>*******</ip-address>
<nas-ip-address>*******</nas-ip-address>
<nas-port>*******</nas-port>
</authentication>
As you can see, there are multiple sets coming back ("authentication"), in this case, two. My code only seems to be pulling one set of data back but I need it to return all (in this case, two). Here is a code snippet. I would sincerely appreciate any help, as I've searched many sites and could not find a solution. What do I need to change?
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text = null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if (name.equals("connection-time")) {
KEY_CONNECTION_TIME = text;
} else if (name.equals("ip-address")) {
KEY_IP_ADDRESS = text;
} else if (name.equals("nas-ip-address")) {
KEY_NAS_IP_ADDRESS = text;
} else if (name.equals("nas-port")) {
KEY_NAS_PORT = text;
} else if(name.equals("authentication-count")) {
KEY_AUTHENTICATION_COUNT = text;
}
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
No comments:
Post a Comment