首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从RSS URL读取rss提要?

如何从RSS URL读取rss提要?
EN

Stack Overflow用户
提问于 2012-02-13 17:14:48
回答 2查看 7.5K关注 0票数 1

我正在开发一款android应用程序,在该应用程序中,我必须从RSS馈送中获取数据,我能够阅读标题、链接和面临的描述问题。如果它是这种格式

代码语言:javascript
运行
复制
<description>     
  worsening of developments in......
</description>

我可以阅读它,但在一些rss提要中也有这种格式

代码语言:javascript
运行
复制
 <description>
    <p><a href="http://news.yahoo.com/ap-sources
 </description>

我看不到这条短信..这是Rss feed的url:http://news.yahoo.com/rss/politics

如何阅读此描述..

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-14 18:06:11

代码语言:javascript
运行
复制
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();
    }

}

Bean是::

代码语言:javascript
运行
复制
package com.samir.XMLParser;

public class HTMLRemoverBean {

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

}
票数 2
EN

Stack Overflow用户

发布于 2012-02-13 17:31:49

当您检测到文本块是HTML时,请在WebView而不是TextView中打开它。我的解决方案如下所示:

代码语言:javascript
运行
复制
WebView wv = (WebView) v.findViewById(R.id.feed_entry_detail);
wv.loadData(mContentFromFeed, "text/html; charset=utf-8", null);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9257949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档