如何阅读RSS来自RSS订阅源的网址?网址、RSS

2023-09-08 08:56:01 作者:钱比心真

我的Andr​​oid应用程序中,我有获取从RSS源数据的工作,我能够读标题,链接和面临的问题与描述。如果是这种格式

 <描述>  在发展恶化......< /描述> 

我能够读出来,但是在一些RSS提要具有这种格式还

 <描述>    &所述p为H.;&下; A HREF =htt​​p://news.yahoo.com/ap-sources < /描述> 

我没有得到这样的文字..这就是RSS订阅网址​​: http://news.yahoo.com/rss/politics 。

如何阅读本说明。

解决方案

 包com.samir.XMLParser;进口java.io. *;进口java.net *。进口的java.util。*;进口javax.xml.parsers中*。org.w3c.dom中导入*。公共类HTMLRemoverParser {    HTMLRemoverBean objBean;    矢量<&HTMLRemoverBean GT; vectParse;    INT mediaThumbnailCount;    布尔urlflag;    诠释计数= 0;    公共HTMLRemoverParser(){        尝试{            vectParse =新的矢量<&HTMLRemoverBean GT;();            网址URL =新的URL(http://news.yahoo.com/rss/politics);            URLConnection的CON = url.openConnection();            的System.out.println(连接是:+ CON);            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(                    con.getInputStream()));            的System.out.println(读者+读卡器);            串inputLine;            串fullStr =;            而((inputLine = reader.readLine())!= NULL)                fullStr = fullStr.concat(inputLine +\\ n);            InputStream中的IStream = url.openStream();            的DocumentBuilder建设者= DocumentBuilderFactory.newInstance()                    .newDocumentBuilder();            文档的DOC = builder.parse(istream的);            doc.getDocumentElement()正常化()。            节点列表NLIST = doc.getElementsByTagName(项目);            的System.out.println();            对于(INT温度= 0;&温度LT; nList.getLength();临时++){                节点nNode = nList.item(临时);                如果(nNode.getNodeType()== Node.ELEMENT_NODE){                    元素eElement =(元)nNode;                    objBean =新HTMLRemoverBean();                    vectParse.add(objBean);                    objBean.title = getTagValue(称号,eElement);                    objBean.description = getTagValue(说明,eElement);                    串noHTMLString = objBean.description.replaceAll(\\\\&所述; * \\\\>,);                    objBean.description = noHTMLString;                    objBean.link = getTagValue(链接,eElement);                    objBean.pubdate = getTagValue(pubdate的eElement);                }            }            对于(INT索引1 = 0;指数1< vectParse.size();索引1 ++){                HTMLRemoverBean ObjNB =(HTMLRemoverBean)vectParse                        获得(索引1);                的System.out.println(编号+指数1);                的System.out.println();                的System.out.println(标题是:+ ObjNB.title);                的System.out.println(描述是:+ ObjNB.description);                的System.out.println(链接是:+ ObjNB.link);                的System.out.println(pubdate的是:+ ObjNB.pubdate);                的System.out.println();                System.out的                        .println(\"-------------------------------------------------------------------------------------------------------------\");            }        }赶上(例外五){            e.printStackTrace();        }    }    私人字符串getTagValue(字符串雄鹿,元素eElement){        节点列表nlList = eElement.getElementsByTagName(雄鹿).item(0)                .getChildNodes();        节点n值=(节点)nlList.item(0);        返回nValue.getNodeValue();    }    公共静态无效的主要(字串[] args){        新HTMLRemoverParser();    }} 
IE浏览器内置的RSS订阅源

和Bean是:

 包com.samir.XMLParser;公共类HTMLRemoverBean {    公共字符串称号;    公共字符串描述;    公共字符串的链接;    公共字符串pubdate的;} 

I am working on an android app in which i have to fetch data from a RSS feed, i am able to read Title, link and facing problem with description. if it is in this format

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

i am able to read it , but in some rss feeds having this format also

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

i am not getting this text.. this is the Rss feed url : http://news.yahoo.com/rss/politics.

how to read this description..

解决方案

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

}

And Bean is ::

package com.samir.XMLParser;

public class HTMLRemoverBean {

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

}