简体   繁体   中英

XMLPullParser android colon tags

I have found several questions of the name question, but can't get any of them to work. What I want is to get the url of the media:thumbnail tag:

<media:thumbnail width="144" height="81" url="http://c.files.bbci.co.uk/6013/production/_88159542_3e6f2bc3-16a3-407d-9e07-62bae1fa755e.jpg"/>

Above the example of such tag

private void handleText(String text) {
            String xmlText = text;
            if (currentEntry != null && currentTag != null) {
                if (currentTag.equals(TITLE)) {
                    currentEntry.setTitle(xmlText);
                } else if (currentTag.equals(DESC)) {
                    currentEntry.setDescription(xmlText);
                } else if (currentTag.equals(LINK)) {
                    currentEntry.setLink(xmlText);
                } else if (currentTag.equals(IMAGE)) {
                    currentEntry.setImage("test");
                }
            }
        }

I tried several things as:

xpp.getAttributeValue(null, "url"); and set the image as that. However I noticed that I am not even getting in that else if clause. I tried several values on the IMAGE variable like:

  • media:thumbnail
  • media
  • thumbnail

I have also set namespace aware:

factory.setNamespaceAware(true);

What am I doing wrong?

parser:

XmlPullParser xpp;
int eventType;

protected List<Entry> doInBackground(String... string) {
    try {

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        xpp = factory.newPullParser();
        xpp.setInput(getInputStream(new URL("http://feeds.bbci.co.uk/news/technology/rss.xml?edition=uk")), "UTF_8");

        eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                handleStartTag(xpp.getName());
            } else if (eventType == XmlPullParser.END_TAG) {
                currentTag = null;
            } else if (eventType == XmlPullParser.TEXT) {
                handleText(xpp.getText());
            }
            eventType = xpp.next();
        }

    } catch (Resources.NotFoundException e) {
        Log.d(LOGTAG, e.getMessage());
    } catch (XmlPullParserException e) {
        Log.d(LOGTAG, e.getMessage());
    } catch (IOException e) {
        Log.d(LOGTAG, e.getMessage());
    }

    return entries;
}

I fixed it. I systemed out the start tags it was parsing and it showed up as: thumbnail. So I changed my IMAGE constant to have the value of "thumbnail". It never came in the thumbnail clause since the handleText method only handles found text in a tag. Since media:thumbnail has no text only attributes with values I needed to handle it in the handleStartTag method. There I could say if the current tag name equals "thumbnail" get the attribute value of url and setImage as that value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM