Reputation: 157
I am trying to do is parse a string which is in a XML format to retrieve certain values. Having looking at multiple XmlPullParser guides I believe the code should be correct, but it doesn't seem to properly look through the string and having run a toast message to see what event type was, it was returning null!
Along with the correct way with looking through the string I wondered how I could retrieve the two values I wanted when the values are part of a start tag, and have commented where in the code I should do this.
Xml string looks like this;
<?xml version="1.0" encoding="UTF-8"><user-name>Bill</user-name><date>ISO 8601 format date</date><trk><name>Run One</name><trkseg><trkpt lat="47" lon="-122"><ele>5</ele><time>2007-10-21T11:23:29</time></trkpt></trkseg>
Here is the code:
try {
routeParse(routeInfo);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public void routeParse(String routeInfo) throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(routeInfo));
int eventType = xpp.getEventType();
while (eventType!=XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType() == XmlPullParser.START_TAG) {
// If tag is trkpt get attribute value lat and lon
}
eventType = xpp.next();
}
}
Thanks for help.
Upvotes: 1
Views: 270
Reputation: 3341
XML parser returns null because your xml is not valid.
Check again what your input xml is.
If you have a typo in question, please update question. Also, paste your errors. Only in that case SO users can help you.
Upvotes: 1