0
私はjavaを使用してthis simple google RSS feedを解析しようとしています。私の目標は、ニュース記事のすべてのタイトルを取得することです。しかし、以下の私のコードはこれをしません。本当にありがとうございます。Java - GoogleニュースのRSSを解析できません。
解析方法:
public static String readLink(String urlAdress) throws IOException {
int i = 0;
URL rssUrl = null;
try {
rssUrl = new URL(urlAdress);
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(rssUrl.openStream()));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String articleTitles = "";
String line = in.readLine();
while (line != null) {
i ++;
System.out.println("Line is not null");
if (line.contains("<title>")) {
System.out.println("Found <title>");
int firstPos = line.indexOf("<title>");
String temp = line.substring(firstPos);
temp = temp.replace("<title>", "");
int lastPos = temp.indexOf("</title>");
temp = temp.substring(0, lastPos);
articleTitles += temp + "\n";
if (i > 6) {
break;
}
}
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return articleTitles;
}
出力:
Text: technology - Google News
technology - Google News
technology - Google News
technology - Google News
technology - Google News
technology - Google News
technology - Google News
*ちょうど私が単にこの行でメソッドを呼び出す方法によって:
System.out.print("Text: " + readLink("https://news.google.com/news/feeds?cf=all&ned=us&hl=en&q=" + word + "&output=rss"));
私はJavaに精通していませんが、[java in java]をチェックすることができます(https://www.google.com.co/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8# q =解析20%20%20%20%サイト20%%3Astackoverflow.com)これらのオプションのいずれかを検討してください。文字列としてXMLコードを解析しているようですが、それだけでは非常に困難です。 –