我正在关注这个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;
}
}
答案 0 :(得分:3)
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";
}
}
}