RSS Feed图片下载

时间:2014-09-24 17:47:56

标签: android dom rss feed

让我们假设我们有以下RSS提要。

  <item> 
  <title>Parliament recalled over IS strikes</title>  
  <description>Parliament will be recalled on Friday over the UK's role in air strikes against 
   Islamic State in Iraq.</description>  
  <link>http://www.bbc.co.uk/news/uk-politics-29339787#sa-ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
  <guid isPermaLink="false">http://www.bbc.co.uk/news/uk-politics-29339787</guid>  
  <pubDate>Wed, 24 Sep 2014 17:23:42 GMT</pubDate>  
  <media:thumbnail width="66" height="49" 
   url="http://news.bbcimg.co.uk/media/images/77797000/jpg/_77797759_77350805.jpg"/>  
  <media:thumbnail width="144" height="81" 
  url="http://news.bbcimg.co.uk/media/images/77797000/jpg/_77797760_77350805.jpg"/> 
</item>  
<item> 
  <title>Obama condemns IS 'network of death'</title>  
  <description>President Obama urges a global drive against the Islamic State's (IS) "network of 
  death", as US air strikes intensify in Iraq and Syria.</description>  
  <link>http://www.bbc.co.uk/news/world-middle-east-29351672#sa-
  ns_mchannel=rss&amp;ns_source=PublicRSS20-sa</link>  
  <guid isPermaLink="false">http://www.bbc.co.uk/news/world-middle-east-29351672</guid>  
  <pubDate>Wed, 24 Sep 2014 16:47:20 GMT</pubDate>  
  <media:thumbnail width="66" height="49" 
   url="http://news.bbcimg.co.uk/media/images/77798000/jpg/_77798505_syridlibreut.jpg"/>  
  <media:thumbnail width="144" height="81" 
   url="http://news.bbcimg.co.uk/media/images/77798000/jpg/_77798506_syridlibreut.jpg"/> 
</item>
</channel>
</rss>

我是如何使用DOM解析器来获取每个标记中的图像的?这是我如何做到这一点但我在我的下载方法中得到一个空指针异常..如果我删除媒体缩略图 标签一切都很好,我可以在listview的每一行看到标题

    ArrayList<MyData> ParseXML(String urlString) throws ParserConfigurationException,
        SAXException, IOException {
    ArrayList<MyData> collection = new ArrayList<MyData>();
    MyData mydata = new MyData();
    URL url = new URL(urlString);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(url.openStream()));
    doc.getDocumentElement().normalize();
    int j;
    NodeList entries = doc.getElementsByTagName("item");
    for (int i = 0; i < entries.getLength(); i++) {
        j = 1;
        Element entry = (Element) entries.item(i);
        NodeList children = entry.getChildNodes();
        for (int k = 0; k < children.getLength(); k++) {
            Node child = children.item(k);
            if ((child.getNodeName().equalsIgnoreCase("title"))
                    //|| (child.getNodeName().equalsIgnoreCase("description"))){
                    //|| (child.getNodeName().equalsIgnoreCase("im:price"))){
                    || ((child.getNodeName().equalsIgnoreCase("media:thumbnail")) && (((Element)  
                         child)
                            .getAttribute("width"))
                            .equalsIgnoreCase("164"))){
                    //|| (child.getNodeName()
                            //.equalsIgnoreCase("im:releaseDate"))) {
                Log.d(child.getNodeName(), child.getTextContent());

                switch (j) {
                case 1:
                    mydata.mTitle = child.getTextContent();
                    break;


                case 2:
                    mydata.mImage = child.getTextContent();
                    break;

                }
                j++;
                if (j == 2) {
                    j = 1;
                    collection.add(mydata);
                    mydata = new MyData();

                }
            }

        }

      }
      return collection;
   }

0 个答案:

没有答案