Created
February 7, 2016 03:22
-
-
Save dougmolineux/a4f4e12affc524bc5cfc to your computer and use it in GitHub Desktop.
Get BBC XML feed and parse it into an object in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var url = 'http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=uk#'; | |
var xml2js = require('xml2js'); | |
var parser = new xml2js.Parser(); | |
var request = require('request'); | |
var colors = require('colors'); | |
// structure of newsItem | |
// { title: [ 'Can Leicester handle title pressure?' ], | |
// description: [ 'Leicester have gone from rank outsiders to title favourites after beating Manchester City - can they cope with the expectation?' ], | |
// link: [ 'http://www.bbc.co.uk/sport/0/football/35514306' ], | |
// guid: [ [Object] ], | |
// pubDate: [ 'Sat, 06 Feb 2016 23:23:36 GMT' ], | |
// 'media:thumbnail': [ [Object], [Object] ] } ] | |
// structure of 'media:thumbnail': | |
// [ { '$': | |
// { width: '66', | |
// height: '37', | |
// url: 'http://c.files.bbci.co.uk/3B3B/production/_88136151_mourinho_index_getty.jpg' } }, | |
request(url, function (error, response, data) { | |
if (!error && response.statusCode == 200) { | |
parser.parseString(data, function (err, result) { | |
console.dir(result.rss.channel[0].item); | |
result.rss.channel[0].item.forEach( newsItem => { | |
console.log('News Item Title: '.green) | |
console.log(newsItem.title[0]); | |
console.log("News Item Description: ".green); | |
console.log(newsItem.description[0]); | |
}); | |
console.log('Done'); | |
}); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment