如何从android中的RSS feed XML中提取图像URL

时间:2021-11-17 21:20:06

Currently I am working on an app that reads in and displays feeds from RSS data, I am able to parse and display the title of the article (the RSS feed is BBC news) and the description of the article, I would also like to be able to add a thumbnail to each entry.

目前我正在开发一个应用程序,它可以读取并显示来自RSS数据的提要,我能够解析并显示文章的标题(RSS提要是BBC新闻)和文章的描述,我也想成为能够为每个条目添加缩略图。

Here is the structure of the XML:

这是XML的结构:

<item> 
  <title>Major earthquake strikes Nepal</title>  
  <description>A magnitude 7.3 earthquake strikes eastern Nepal, two weeks after a devastating quake that killed more than 8,000 people.</description>  
  <link>http://www.bbc.co.uk/news/world-asia-32701385#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
  <guid isPermaLink="false">http://www.bbc.co.uk/news/world-asia-32701385</guid>  
  <pubDate>Tue, 12 May 2015 11:37:06 GMT</pubDate>  
  <media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/82937000/jpg/_82937953_b060dce4-16b1-4c1f-8cb5-412a744930f9.jpg"/>  
  <media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/82937000/jpg/_82937954_b060dce4-16b1-4c1f-8cb5-412a744930f9.jpg"/> 
</item>  

I need the URL from the <media:thumbnail> tag

我需要 标签中的URL

Here is my java code to get the title and description:

这是我的java代码来获取标题和描述:

int eventType = xpp.getEventType();

int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("title")) {
                    if (insideItem)

                        title =xpp.nextText();



                } else if (xpp.getName().equalsIgnoreCase("description")) {
                    if (insideItem)
                      description = xpp.nextText();

                                         }
            } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
                insideItem = false;
                Item item = new Item(title, description, "URL HERE");

                headlines.add(item);
            }

            eventType = xpp.next(); //move to next element


        }

Item is a class that contains the title,description and URL getters and setters in order to display the data in various textviews and imageviews

Item是一个包含标题,描述和URL getter和setter的类,以便在各种textview和imageview中显示数据

1 个解决方案

#1


0  

Maybe you could use something like this:

也许你可以使用这样的东西:

String url = "";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document document = db.parse(is);
NodeList nodeList = document.getElementsByTagName("Item");
for(int i=0; i<nodeList.getLength(); i++) {
    url = nodeList.item(i).getAttributes().getNamedItem("url").getNodeValue();
} //or if it is always only one item -> url = nodeList.item(0).getAttributes().getNamedItem("url").getNodeValue();

and then you can set the url to your Item object

然后您可以将URL设置为Item对象

#1


0  

Maybe you could use something like this:

也许你可以使用这样的东西:

String url = "";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document document = db.parse(is);
NodeList nodeList = document.getElementsByTagName("Item");
for(int i=0; i<nodeList.getLength(); i++) {
    url = nodeList.item(i).getAttributes().getNamedItem("url").getNodeValue();
} //or if it is always only one item -> url = nodeList.item(0).getAttributes().getNamedItem("url").getNodeValue();

and then you can set the url to your Item object

然后您可以将URL设置为Item对象