-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Example usage | ||
const rssUrl = 'https://feeds.bloomberg.com/markets/news.rss'; | ||
fetchAndParseRSS(rssUrl); | ||
|
||
async function fetchAndParseRSS(url) { | ||
try { | ||
const response = await fetch(url); | ||
const rssString = await response.text(); | ||
parseRSS(rssString); | ||
} catch (error) { | ||
console.error('Error fetching or parsing RSS:', error); | ||
} | ||
} | ||
|
||
function parseRSS(rssString) { | ||
const parser = new DOMParser(); | ||
const xmlDoc = parser.parseFromString(rssString, "text/xml"); | ||
|
||
const channel = xmlDoc.getElementsByTagName("channel")[0]; | ||
const items = channel.getElementsByTagName("item"); | ||
|
||
const feedTitle = channel.getElementsByTagName("title")[0].textContent; | ||
const feedDescription = channel.getElementsByTagName("description")[0].textContent; | ||
|
||
console.log(`Feed Title: ${feedTitle}`); | ||
console.log(`Feed Description: ${feedDescription}`); | ||
console.log("Latest News Items:"); | ||
|
||
for (let i = 0; i < items.length; i++) { | ||
const title = items[i].getElementsByTagName("title")[0].textContent; | ||
const description = items[i].getElementsByTagName("description")[0].textContent; | ||
const pubDate = items[i].getElementsByTagName("pubDate")[0].textContent; | ||
|
||
console.log(`Title: ${title}`); | ||
console.log(`Description: ${description}`); | ||
console.log(`Publication Date: ${pubDate}`); | ||
console.log("-----------------------------------"); | ||
} | ||
} |