如何从Android中的RSS进料XML提取图像URL



当前我正在研究一个读取并显示从RSS数据显示的应用程序,我能够解析和显示文章的标题(RSS feed是BBC新闻)和文章的描述,我还希望能够为每个条目添加一个缩略图。

这是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>  

我需要<media:thumbnail> TAG

的URL

这是我的Java代码,以获取标题和描述:

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

        }

项目是包含标题,描述和URL获取器和设置器的类,以便在各种文本视图和ImageViews中显示数据

也许您可以使用类似的东西:

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();

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

相关内容

  • 没有找到相关文章

最新更新