1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry but could you help me understand why my XML is not valid?
'<trk>' is not ended in your XML. I highly recommend you to check your XML with a XML validator (you choose what validator you preffer).
Awesome I got the XML to parse after validating the XML, a lesson learned :) Any pointers to get the two attribute values from a tag?
0

As you can see tag trk is not ended in your XML.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.