2012-02-13 160 views
0

我正在一個android應用程序中,我必須從RSS提要中獲取數據,我能夠讀取標題,鏈接和麪對問題的描述。如果是這種格式如何從rss URL讀取RSS提要?

<description>  
    worsening of developments in...... 
</description> 

我能夠讀出來,但是在一些RSS提要具有這種格式也

<description> 
    <p><a href="http://news.yahoo.com/ap-sources 
</description> 

我沒有得到這個文本.. 這就是RSS訂閱網址:http://news.yahoo.com/rss/politics

如何仔細閱讀本說明..

+0

我有解析器刪除此標籤,但你肯定第一個描述如..羅姆尼和他的資金不足對手正在利用共和黨總統候選人提名鬥爭持續一週的時間 - 無論是辯論還是初選 - 提高實施超級星期二策略所需的資金並在以後的國家進行競爭。 – 2012-02-14 09:20:43

+0

是的,這是描述。我想同時獲得該字符串,如果我想要得到的正常格式的描述也.. – 2012-02-14 09:51:56

+0

看到我的答案,它有兩個類(HTMLRemoverParser,HTMLRemoverBean)類項目和解析。在java.Cheers中! – 2012-02-14 10:07:47

回答

2
package com.samir.XMLParser; 

import java.io.*; 
import java.net.*; 
import java.util.*; 
import javax.xml.parsers.*; 
import org.w3c.dom.*; 

public class HTMLRemoverParser { 

    HTMLRemoverBean objBean; 
    Vector<HTMLRemoverBean> vectParse; 

    int mediaThumbnailCount; 
    boolean urlflag; 
    int count = 0; 

    public HTMLRemoverParser() { 
     try { 

      vectParse = new Vector<HTMLRemoverBean>(); 
      URL url = new URL("http://news.yahoo.com/rss/politics"); 
      URLConnection con = url.openConnection(); 

      System.out.println("Connection is : " + con); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(
        con.getInputStream())); 
      System.out.println("Reader :" + reader); 

      String inputLine; 
      String fullStr = ""; 
      while ((inputLine = reader.readLine()) != null) 
       fullStr = fullStr.concat(inputLine + "\n"); 

      InputStream istream = url.openStream(); 

      DocumentBuilder builder = DocumentBuilderFactory.newInstance() 
        .newDocumentBuilder(); 

      Document doc = builder.parse(istream); 

      doc.getDocumentElement().normalize(); 


      NodeList nList = doc.getElementsByTagName("item"); 

      System.out.println(); 

      for (int temp = 0; temp < nList.getLength(); temp++) { 

       Node nNode = nList.item(temp); 
       if (nNode.getNodeType() == Node.ELEMENT_NODE) { 

        Element eElement = (Element) nNode; 

        objBean = new HTMLRemoverBean(); 
        vectParse.add(objBean); 

        objBean.title = getTagValue("title", eElement); 
        objBean.description = getTagValue("description", eElement); 
        String noHTMLString = objBean.description.replaceAll("\\<.*?\\>", ""); 
        objBean.description=noHTMLString; 
        objBean.link = getTagValue("link", eElement); 
        objBean.pubdate = getTagValue("pubDate", eElement); 

       } 
      } 

      for (int index1 = 0; index1 < vectParse.size(); index1++) { 
       HTMLRemoverBean ObjNB = (HTMLRemoverBean) vectParse 
         .get(index1); 

       System.out.println("Item No : " + index1); 
       System.out.println(); 

       System.out.println("Title is : " + ObjNB.title); 
       System.out.println("Description is : " + ObjNB.description); 
       System.out.println("Link is : " + ObjNB.link); 
       System.out.println("Pubdate is : " + ObjNB.pubdate); 

       System.out.println(); 
       System.out 
         .println("-------------------------------------------------------------------------------------------------------------"); 

      } 

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

    private String getTagValue(String sTag, Element eElement) { 
     NodeList nlList = eElement.getElementsByTagName(sTag).item(0) 
       .getChildNodes(); 

     Node nValue = (Node) nlList.item(0); 

     return nValue.getNodeValue(); 

    } 

    public static void main(String[] args) { 
     new HTMLRemoverParser(); 
    } 

} 

和豆腐是::

package com.samir.XMLParser; 

public class HTMLRemoverBean { 

    public String title; 
    public String description; 
    public String link; 
    public String pubdate; 

} 
+0

通過使用這個類,我能夠讀取所有東西.. – 2012-02-14 10:26:19

+0

在兩個班裏都好嗎? – 2012-02-14 10:28:10

+0

是的,我正在使用這兩個類,現在我試圖在列表視圖中顯示這些項目。 – 2012-02-14 10:36:04

1

當檢測到的文本塊是HTML,在網頁視圖,而不是一個TextView打開它。我的解決辦法是這樣的:

WebView wv = (WebView) v.findViewById(R.id.feed_entry_detail); 
wv.loadData(mContentFromFeed, "text/html; charset=utf-8", null); 
+0

我只能使用textview,因爲我能夠顯示列表視圖中的項目信息.. – 2012-02-13 09:33:40