Maxime M
Maxime M

Reputation: 53

XMLPullParser with prefix and self closing tag

I have an issue on XML parsing with XMLPullParser on Android since few days. I'm trying to parse this :

<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-6JMmPoQTJOc/UoYaJT9Ih9I/AAAAAAAAE7I/mnO_69i8rAs/s72-c/ANTICIPA-Logo.png" height="72" width="72"/>

I want to get the image URL, which I successfully do. However, I am not able to detect the END_TAG, and I don't know how to do it.

Here is my parsing code :

    private String readImage(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "media:thumbnail");
        String imageLink = parser.getAttributeValue(null, "url");
        parser.require(XmlPullParser.END_TAG, ns, "thumbnail"); // THIS LINE HAS A PROBLEM
        Log.d("DEBUG", imageLink);
        return imageLink;
    }

I tried to give "thumbnail", "media:thumbnail", "/", nothing seems to work. Do you have an idea ?

Upvotes: 2

Views: 1614

Answers (1)

Matthew Burke
Matthew Burke

Reputation: 2364

You cannot detect the end tag because there is no end tag: the thumbnail tag is self-closing. In your parsing code, when you call parser.require for the second time (line 4 of your code snippet) you are saying If I am not now at an end tag, throw an exception. So, of course it throws an exception.

The solution is simple: remove that second call to parser.require.

Upvotes: 1

Related Questions