Java阅读RSS Feed

时间:2015-08-21 07:31:24

标签: java rss

我正在关注这个Youtube tutorial,但是当他从CNN RSS获得所有头条新闻时,我只获得了1个标题。为什么会这样?

我的代码(就我所见,与教程中的代码相同)

import java.net.MalformedURLException;
import java.net.URL;
import java.io.*;


public class ReadRSS {

    public static void main(String[] args) {

        System.out.println(readRSSFeed("http://rss.cnn.com/rss/edition.rss"));
    }

    public static String readRSSFeed(String urlAddress){
        try{
            URL rssUrl = new URL (urlAddress);
            BufferedReader in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
            String sourceCode = "";
            String line;
            while((line=in.readLine())!=null){
                if(line.contains("<title>")){
                    System.out.println(line);
                    int firstPos = line.indexOf("<title>");
                    String temp = line.substring(firstPos);
                    temp=temp.replace("<title>","");
                    int lastPos = temp.indexOf("</title>");
                    temp = temp.substring(0,lastPos);
                    sourceCode +=temp+ "\n" ;
                }
            }
            in.close();
            return sourceCode;
        } catch (MalformedURLException ue){
            System.out.println("Malformed URL");
        } catch (IOException ioe){
            System.out.println("Something went wrong reading the contents");
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:3)

自从他制作了Youtube视频后,CNN的Feed格式发生了变化。代码假设每行有一个标题标记,实际上有多个标题标记。这样的事情现在应该有效:

while ((line = in.readLine()) != null) {
    int titleEndIndex = 0;
    int titleStartIndex = 0;
    while (titleStartIndex >= 0) {
        titleStartIndex = line.indexOf("<title>", titleEndIndex);
        if (titleStartIndex >= 0) {
            titleEndIndex = line.indexOf("</title>", titleStartIndex);
            sourceCode += line.substring(titleStartIndex + "<title>".length(), titleEndIndex) + "\n";
        }
    }
}