如何从rss feed中读取包含标题,图片和网址的<img/>标记

时间:2018-01-18 17:22:42

标签: android xml-parsing rss

我能够获取项目标签内的所有数据,即Title,Link,Description,Image和PublishedDate,下面是我的XMLParser类

        if (eventType == XmlPullParser.START_TAG) {

            if (xmlPullParser.getName().equalsIgnoreCase("item")) {

                insideItem = true;

            } else if (xmlPullParser.getName().equalsIgnoreCase("title")) {

                if (insideItem) {
                    String title = xmlPullParser.nextText();
                    currentArticle.setTitle(title);
                }

            } else if (xmlPullParser.getName().equalsIgnoreCase("link")) {

                if (insideItem) {
                    String link = xmlPullParser.nextText();
                    currentArticle.setLink(link);
                }

            } else if (xmlPullParser.getName().equalsIgnoreCase("description")) {

                if (insideItem) {
                    String description = xmlPullParser.nextText();
                    currentArticle.setDescription(description);
                }

            }  else if (xmlPullParser.getName().equalsIgnoreCase("media:thumbnail")) {

                if (insideItem) {
                    String htmlData = xmlPullParser.nextText();
                    Document doc = Jsoup.parse(htmlData);
                    try {
                        //Get image url from media:content tag
                        String pic = xmlPullParser.getAttributeValue(null, "url");
                        currentArticle.setImage(pic);
                    } catch (NullPointerException e) {
                        currentArticle.setImage(null);
                    }
                    currentArticle.setContent(htmlData);
                }

            } else if (xmlPullParser.getName().equalsIgnoreCase("description")) {

                if (insideItem) {
                    String description = xmlPullParser.nextText();
                    currentArticle.setDescription(description);
                }

            } else if (xmlPullParser.getName().equalsIgnoreCase("pubDate")) {
                @SuppressWarnings("deprecation")
                Date pubDate = new Date(xmlPullParser.
                        nextText());

                currentArticle.setPubDate(pubDate);
            }

        }
            else if (eventType == XmlPullParser.END_TAG && xmlPullParser.getName().equalsIgnoreCase("item")) {
            insideItem = false;
            insideChannel = false;
            articles.add(currentArticle);
            currentArticle = new Article();
        }
        eventType = xmlPullParser.next();

我的问题是如何读取商品标签和图片标签内的数据。我知道我必须纠正我的条件陈述,尝试了很多,但我找不到任何解决方案

 <image>
        <url>http://news.bbcimg.co.uk/nol/shared/img/bbc_news_120x60.gif</url>
        <title>BBC News - World</title>
        <link>http://www.bbc.co.uk/news/</link>
 </image>

1 个答案:

答案 0 :(得分:0)

解析XML文件有三种方法。使用DOM Parser解析XML文件很简单,但它比其他解析器慢,所以XMLPullParser比DOM更好,你必须解析每个解析器的不同方式。

您将在下面的链接中找到问题的解决方案。

https://www.journaldev.com/10653/android-xml-parser-xmlpullparser

相关问题