2013-04-07 19 views
0

這是一個從我剛纔的問題繼續在這裏的問題:RSS Reader NullPointerExceptionRSS閱讀器沒有得到一些標籤

在我的應用程序,在我的名單,有時我不明白的RSS的標題,有時描述(和圖像)。這裏最奇怪的是,我沒有問題的所有鏈接。例如,如果我解析原始教程的鏈接(http://www.mobilenations.com/rss/mb.xml),一切正常。但是當我使用的其他鏈接我有上述的問題...

這是我的DOMParser類:

package com.td.rssreader.parser; 

import java.net.MalformedURLException; 
import java.net.URL; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import org.jsoup.Jsoup; 
import org.jsoup.select.Elements; 
import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.InputSource; 

public class DOMParser { 

    private RSSFeed _feed = new RSSFeed(); 

    public RSSFeed parseXml(String xml) { 

     // _feed.clearList(); 

     URL url = null; 
     try { 
      url = new URL(xml); 
     } catch (MalformedURLException e1) { 
      e1.printStackTrace(); 
     } 

     try { 
      // Create required instances 
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 

      // Parse the xml 
      Document doc = db.parse(new InputSource(url.openStream())); 
      doc.getDocumentElement().normalize(); 

      // Get all <item> tags. 
      NodeList nl = doc.getElementsByTagName("item"); 
      int length = nl.getLength(); 

      for (int i = 0; i < length; i++) { 
       Node currentNode = nl.item(i); 
       RSSItem _item = new RSSItem(); 

       NodeList nchild = currentNode.getChildNodes(); 
       int clength = nchild.getLength(); 

       // Get the required elements from each Item 
       for (int j = 1; j < clength; j = j + 2) { 

        Node thisNode = nchild.item(j); 
        String theString = null; 

         if (thisNode != null && thisNode.getFirstChild() != null) { 
          theString = thisNode.getFirstChild().getNodeValue(); 
         } 


        if (theString != null) { 

         String nodeName = thisNode.getNodeName(); 

         if ("title".equals(nodeName)) { 
          // Node name is equals to 'title' so set the Node 
          // value to the Title in the RSSItem. 
          _item.setTitle(theString); 
         } 

         else if ("description".equals(nodeName)) { 
          _item.setDescription(theString); 

          // Parse the html description to get the image url 
          String html = theString; 
          org.jsoup.nodes.Document docHtml = Jsoup 
            .parse(html); 
          Elements imgEle = docHtml.select("img"); 
          _item.setImage(imgEle.attr("src")); 
         } 
//description 
         else if ("pubDate".equals(nodeName)) { 

          // We replace the plus and zero's in the date with 
          // empty string 
          String formatedDate = theString.replace(" +0000", 
            ""); 
          _item.setDate(formatedDate); 
         } 


         if ("link".equals(nodeName)) { 
          // Node name is equals to 'title' so set the Node 
          // value to the Title in the RSSItem. 
          _item.setLink(theString); 
         } 
        } 
       } 

       // add item to the list 
       _feed.addItem(_item); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // Return the final feed once all the Items are added to the RSSFeed 
     // Object(_feed). 
     return _feed; 
    } 

} 

回答

1

一旦你通過項目節點的循環,你再有另一個循環試圖遍歷子元素(設置標題,描述等)。

但你循環開始於索引1,並增加2:

// Get the required elements from each Item 
      for (int j = 1; j < clength; j = j + 2) { 

這意味着它只檢查位置1,3,5等

望着XML發佈,昭示着爲什麼你得到不同的數據每個項目。設置循環索引爲0,並增加1。

+0

你是最棒的!!我一直在這裏找幾個小時!!!! :)非常感謝你們!!!! :) – 2013-04-07 21:04:37