jquery - Parsing Google News RSS with Javascript -
want parse google news rss javascript. managed run code in php code form how parse google news in php:
my question how can same thing in javascript code example parse: http://news.google.com/news?pz=1&output=rss&cf=all&topic=topics&q=obama&hl=en&num=10 , data different news there
thanks.
the data obtained in xml format.
you can access items using domparser
. see: xml parser
example
parser = new domparser(); xmldoc = parser.parsefromstring(data,"text/xml"); var items = xmldoc.getelementsbytagname('item') console.log(items);
📝 working example
// [1] parse xml feed array of js objects function parsedata(data) { parser = new domparser(); var xmldoc = parser.parsefromstring(data,"text/xml"); // convert collection array // getelementsbytagname returns collection/nodelist instead of array. // more convenient if use array. var items = array.from(xmldoc.getelementsbytagname('item')); var feeds = []; items.foreach(function (item) { feeds.push({ title: getnode(item, 'title'), link: getnode(item, 'link'), pubdate: getnode(item, 'pubdate'), description: getnode(item, 'description'), }); }); return feeds; } // retrieve data of specific tag function getnode(node, tagtoretrieve) { var htmldata = node.getelementsbytagname(tagtoretrieve)[0].innerhtml; return _.unescape(htmldata); // decode html entities, see lodash/underscore }
see demo: https://jsfiddle.net/amoshydra/ao0tacs6/
Comments
Post a Comment