Java - Parsing Google News RSS Not Working


Java - Parsing Google News RSS Not Working



I am trying to parse this simple google RSS feed using java. My goal is to get all the titles of the news articles. My code below, however, does not do this. I would really appreciate your help, thanks.



Parsing Method:


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;
}



The Output:


Text: technology - Google News
technology - Google News
technology - Google News
technology - Google News
technology - Google News
technology - Google News
technology - Google News



*Just by the way I simply call the method with this line:


System.out.print("Text: " + readLink("https://news.google.com/news/feeds?cf=all&ned=us&hl=en&q=" + word + "&output=rss"));





I'm not familiar with java, but you can check parse XML in java Consider any of those options. It seems that you're parsing the XML code as a string, but that's very difficult alone.
– Mauricio Arias Olave
Sep 21 '16 at 20:34




2 Answers
2



I know you might want to do this manually but I thought I'd mention that jsoup is a great tool for parsing html!



Bro, why are you sticking with RSS feed. NewsAPI JSON can help you to get latest news. You don't need to parse json. There is a library called Easy News, It gives the latest news by calling the method itself. No need to parse JSON. You can try it.


List <Map> list=EasyNews.getNewsByKeywords("bitcoin");



This line will return list of news based on query:


{"status":"ok","totalResults":156210,"articles":[{"source":{"id":null,"name":"Youbrandinc.com"},"author":"Scott Scanlon","title":"Bitcoin Bitcoin Bitcoin BITCOIN!! So Sue Us.","description":"Crypto Briefing exists to advocate for the safe and responsible integration of blockchain and cryptocurrency into mainstream life. We believe. Our goal is to grow the crypto community – to help new converts understand the basics, and to help more experienced …","url":"https://www.youbrandinc.com/crytocurrency/bitcoin-bitcoin-bitcoin-bitcoin-so-sue-us/","urlToImage":"https://www.youbrandinc.com/wp-content/uploads/2018/05/Bitcoin-Trademarked-In-The-UK-In-Patent-Troll-Style-Action-1024x538.jpg","publishedAt":"2018-05-31T01:03:10Z"},{"source":{"id":null,"name":"Dailyfx.com"},"author":"Valentin Aufrand, Analyste, Valentin Aufrand","title":"Bitcoin, Bitcoin Cash, Bitcoin Gold, quelles différences ?","description":"Après le Bitcoin Cash, c’est le Bitcoin Gold qui s’est vu créé le mardi 24 octobre suite à une division entre la communauté Bitcoin.","url":"https://www.dailyfx.com/francais/actualite_forex_trading/fondamentaux/rapports_speciaux/2017/10/26/Bitcoin-Bitcoin-Cash-Bitcoin-Gold-quelles-differences.html","urlToImage":null,"publishedAt":"2017-10-26T10:30:00Z"},{"source":{"id":null,"name":"Github.com"},"author":"cl-bitcoin","title":"cl-bitcoin: cl-bitcoin","description":"cl-bitcoin - Peer to peer money","url":"https://github.com/cl-bitcoin/cl-bitcoin","urlToImage":"https://avatars0.githubusercontent.com/u/39051405?s=400&v=4","publishedAt":"2018-05-07T10:08:22Z"},{"source":{"id":null,"name":"Bleepingcomputer.com"},"author":"ergrgergreg","title":"Bitcoin SuPpOrt 18005716109 Bitcoin","description":"Bitcoin SuPpOrt 18005716109 Bitcoin - posted in Windows 10 Support: Bitcoin SuPpOrt 18005716109 Bitcoinn nBitcoin SuPpOrt 18005716109 Bitcoinn nBitcoin SuPpOrt 18005716109 Bitcoinn nBitcoin SuPpOrt 18005716109 Bitcoinn nBitcoin SuPpOrt 18005716109 Bitcoinn nB…","url":"https://www.bleepingcomputer.com/forums/t/673721/bitcoin-support-18005716109-bitcoin/","urlToImage":"https://www.bleepingcomputer.com/forums/public/style_images/master/meta_image.png","publishedAt":"2018-03-21T15:46:13Z"},{"source":{"id":null,"name":"Linux.org.ru"},"author":"cheetah111v","title":"Bitcoin","description":"Много ли на ЛОРе интересующихся или даже может быть участников разработки или даже может быть инвесторов криптовалюты? bitcoin","url":"https://www.linux.org.ru/forum/talks/14253680","urlToImage":"https://www.linux.org.ru/img/good-penguin.png","publishedAt":"2018-06-01T09:03:57Z"},{"source":{"id":null,"name":"Newsbtc.com"},"author":"Joseph Young","title":"44 Bitcoin Forks Have Emerged Within 10 Months, Crypto Investors Not Convinced","description":"Bitcoin Private, Bitcoin Diamond, Bitcoin Gold, Bitcoin Atom, Bitcoin Candy, and Bitcoin Pizza are some of the 44 bitcoin forks that have emerged since the initial Bitcoin Cash hard fork in August of last year, as TNW reported. Within the past 10 months, 44 f…","url":"https://www.newsbtc.com/2018/06/25/44-bitcoin-forks-emerged-within-10-months-investors-not-taking-seriously/","urlToImage":"https://s3.amazonaws.com/main-newsbtc-images/2018/06/25033022/shutterstock_685257826.jpg","publishedAt":"2018-06-25T08:00:51Z"},{"source":{"id":null,"name":"Hackernoon.com"},"author":"BambouClub","title":"Bitcoin Has Cashflow: Lending Bitcoin","description":"There is a fallacy that Bitcoin has no CashFlow. That’s what Warren Buffet thinks.","url":"https://hackernoon.com/bitcoin-has-cashflow-lending-bitcoin-75733438f01a","urlToImage":"https://cdn-images-1.medium.com/max/1200/1*8F0GleFn0N-61Rf9u5V13w.png","publishedAt":"2018-05-21T01:49:44Z"},{"source":{"id":null,"name":"Deviantart.com"},"author":null,"title":"Bitcoin","description":"","url":"https://merrchantfintec.deviantart.com/art/Bitcoin-751093874","urlToImage":"https://orig00.deviantart.net/01da/f/2018/174/3/b/bitcoin_by_merrchantfintec-dcf6jqq.jpg","publishedAt":"2018-06-23T07:24:10Z"},{"source":{"id":null,"name":"Dribbble.com"},"author":"Alexei Vella","title":"Bitcoin","description":"© Alexei Vella. nPress Z to ZOOM!","url":"https://dribbble.com/shots/4756919-Bitcoin","urlToImage":"https://cdn.dribbble.com/users/768890/screenshots/4756919/bct.jpg","publishedAt":"2018-06-27T14:01:37Z"},{"source":{"id":null,"name":"Thingiverse.com"},"author":"KhanKudo","title":"Bitcoin","description":"Got bored and quickly made a bitcoin.I suggest painting or printing it in gold.","url":"https://www.thingiverse.com/thing:2982578","urlToImage":"https://cdn.thingiverse.com/renders/dc/f7/c9/37/5a/05f3d4f49db5b3482f6468551c2697ca_preview_featured.jpg","publishedAt":"2018-06-29T09:07:57Z"},{"source":{"id":null,"name":"Levelup.com"},"author":"Staff LevelUp","title":"BitCoin","description":"Analizamos qué rayos es y cómo funciona la moneda del futuro que está en boca de todos Continuar leyendo »","url":"http://www.levelup.com/shows/Que-rayos-es/444037/BitCoin","urlToImage":"http://s3.amazonaws.com/www.levelup.com/public/uploads/images/527653_200x200.jpg","publishedAt":"2017-10-21T17:05:00Z"},{"source":{"id":null,"name":"Sostav.ru"},"author":null,"title":"Bitcoin","description":"Cryptocurrencies are gaining more and more popularity and on the background of this interest in them by the ordinary web users is justified. Is it really worth to get there if you do not understand anything? The answer is very simple - only the latest news in…","url":"http://www.sostav.ru/blogs/132684/23861/","urlToImage":null,"publishedAt":"2017-10-28T06:34:12Z"},{"source":{"id":null,"name":"Maketecheasier.com"},"author":"Andrew Braun","title":"Why Are There So Many Bitcoin Variants?","description":"There are plenty of cryptocurrencies around, and you can even create one yourself in minutes. Out of all these cryptocurrencies, there are plenty that come with the “Bitcoin” label that you will think are associated with Bitcoin. Think you have a handle on th…","url":"https://www.maketecheasier.com/why-so-many-bitcoin-variants/","urlToImage":"https://www.maketecheasier.com/assets/uploads/2018/05/bitcoin-types-feature.jpg","publishedAt":"2018-05-25T05:25:25Z"},{"source":{"id":"vice-news","name":"Vice News"},"author":"Jordan Pearson, Kate Lunau, Jason Koebler","title":"A Second Bitcoin Fork Is Looming and Battle Lines Are Being Drawn","description":"Bitcoin, Bitcoin Cash, and soon... Bitcoin Gold.","url":"https://motherboard.vice.com/en_us/article/mb3eja/a-second-fork-bitcoin-gold-is-looming-battle-lines-being-drawn","urlToImage":"https://video-images.vice.com/articles/59ee371b130e601dff1258c7/lede/1508784566985-shutterstock_400744033.jpeg?crop=1xw:0.8440514469453376xh;center,center&resize=1200:*","publishedAt":"2017-10-23T18:50:06Z"},{"source":{"id":null,"name":"Dribbble.com"},"author":"virender","title":"Bitcoin","description":"Bitcoin Flat IconsnnBuy this Iconsnhttps://www.iconfinder.com/iconsets/bitcoin-51","url":"https://dribbble.com/shots/4600055-Bitcoin","urlToImage":"https://cdn.dribbble.com/users/1607724/screenshots/4600055/bitcoin.png","publishedAt":"2018-05-17T04:46:00Z"},{"source":{"id":null,"name":"Dribbble.com"},"author":"Aleksandr Reva","title":"Bitcoin","description":"View on Dribbble","url":"https://dribbble.com/shots/4644508-Bitcoin","urlToImage":"https://cdn.dribbble.com/users/275406/screenshots/4644508/bitcoin_block_chain.png","publishedAt":"2018-05-29T12:07:08Z"},{"source":{"id":null,"name":"Yaplakal.com"},"author":"adm@yaplakal.com (МоркоФФка)","title":"Bitcoin","description":"только что сбросили UPD - весь отдел в слезах","url":"https://www.yaplakal.com/forum2/topic1670115.html","urlToImage":"https://s00.yaplakal.com/pics/pics_original/9/5/3/10380359.jpg","publishedAt":"2017-10-04T07:30:04Z"},{"source":{"id":null,"name":"Pcgameshardware.de"},"author":"BloodSteam","title":"Bitcoin","description":"Hallo, ich hab hier ein bisschen in Forum gesucht und leider kein Sammelthread �ber Bitcoins gefunden. Finde Bitcoins sehr interessant, nicht weil man","url":"http://extreme.pcgameshardware.de/wirtschaft-politik-und-wissenschaft/489880-bitcoin.html","urlToImage":null,"publishedAt":"2017-10-08T12:48:45Z"},{"source":{"id":null,"name":"Dribbble.com"},"author":"Parham Marandi","title":"Bitcoin","description":"Instagram | Twitter | Facebook","url":"https://dribbble.com/shots/3876171-Bitcoin","urlToImage":"https://cdn.dribbble.com/users/1061799/screenshots/3876171/bitcoinreq.png","publishedAt":"2017-10-16T01:29:31Z"},{"source":{"id":null,"name":"Thingiverse.com"},"author":"thatburritoth0","title":"Bitcoin","description":"a little bitcoin I designed in inventor","url":"https://www.thingiverse.com/thing:2595467","urlToImage":"https://cdn.thingiverse.com/renders/c1/e0/52/c8/b8/3d30b3981b3137fed63c9e5089f43abb_preview_featured.jpg","publishedAt":"2017-10-19T20:32:30Z"}]}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages