2016-09-20 83 views
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")); 
+0

我對java不熟悉,但可以查看[解析java中的XML](https://www.google.com.co/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8# q = parse%20xml%20in%20java%20site%3Astackoverflow.com)考慮這些選項中的任何一個。您似乎將XML代碼解析爲一個字符串,但這非常困難。 –

回答

0

我知道你可能會想手動這樣做,但我想我會提到,jsoup是一個偉大的解析HTML工具!