Categories
JavaScript Answers

How to Parse an RSS Feed Using JavaScript?

Spread the love

We can use the DOMParser constructor to parse RSS strings in JavaScript.

For instance, we can write:

const htmlTxt = `
<?xml version="1.0" ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>LibriVox's New Releases</title>
  <link>http://librivox.org</link>
  <atom:link href="https://librivox.org/rss/latest_releases" rel="self" type="application/rss+xml" />
  <description>LibriVox volunteers record chapters of books in the public domain and release the audio files back onto the net. Our goal is to make all public domain books available as free audio books. We are a totally volunteer, open source, free content, public domain project.</description>
  <!-- project loop -->
     <item>
    <title><![CDATA[Endeavour Journal of Sir Joseph Banks from 25 August 1768-12 July 1771, The by Joseph Banks (1743 - 1820)]]></title>
    <description><![CDATA[In this Journal, Joseph Banks records almost daily observations of the journey of the ship the Endeavour on the first of James Cook’s voyages to the Pacific during the years 1768-1771. There are also more detailed accounts of the events, people, flora, fauna and geology of the places (except Brazil) where they landed. They landed at Brazil, Tahiti, New Zealand, Australia, Batavia, Cape Town and St. Helena.

Joseph Banks was one of the naturalists on the Endeavour, appointed by the Royal Society. The joint Royal Society, Royal Navy journey of the Endeavour was overtly a scientific expedition with the stated purpose of observing the transit of Venus from Tahiti. The other purpose of the journey was to attempt the discovery of the postulated Southern Continent (Terra Australis). In addition to himself, Banks funded the inclusion on the voyage of two other naturalists (Solander and Spöring) and two artists (Parkinson and Buchan).

Joseph Banks became famous upon the return to Britain of the Endeavour and went on to be a highly influential person in Britain. Banks was President of the British Royal Society for more than 40 years; and was a strong advocate for the settlement of New South Wales as a convict settlement. Along with James Cook, he was responsible for representing the Australian continent as terra nullius (“nobody’s land”) even though he observed the occupation of the land by the indigenous Australians, which misrepresentation contributed to the colonization of Australia by the British. Now, 250 years after the Endeavour's voyage, controversy and the voices of indigenous people are leading to a reassessment of the effects of James Cook’s voyages and the colonial legacy of Australia and New Zealand. (Gail Timmerman-Vaughan) ]]></description>
    <link><![CDATA[https://librivox.org/the-endeavour-journal-of-sir-joseph-banks-from-25-august-1768-12-july-1771-by-joseph-banks/]]></link>
    <guid><![CDATA[https://librivox.org/the-endeavour-journal-of-sir-joseph-banks-from-25-august-1768-12-july-1771-by-joseph-banks/]]></guid>
    <pubDate><![CDATA[Sat, 15 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[Old New York by Edith Wharton (1862 - 1937)]]></title>
    <description><![CDATA[Old New York (1924) is a collection of four novellas (False Dawn; The Old Maid; The Spark; New Year's Day)  by Edith Wharton, revolving around upper-class New York City society in the 1840s, 1850s, 1860s, and 1870s. - Summary from Wikipedia]]></description>
    <link><![CDATA[https://librivox.org/old-new-york-by-edith-wharton/]]></link>
    <guid><![CDATA[https://librivox.org/old-new-york-by-edith-wharton/]]></guid>
    <pubDate><![CDATA[Fri, 14 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[Tale of Bunny Cotton-Tail, The by Laura Rountree Smith (1876 - 1924)]]></title>
    <description><![CDATA[Juvenile literature -- Conduct of life, Children's poetry -- 1905, Conduct of life -- Juvenile literature, Friendship -- Juvenile literature, Obedience -- Juvenile literature, Parent and child -- Juvenile literature, Rabbits -- Juvenile literature, Children's literature, Conduct of life -- Juvenile fiction, Rabbits -- Juvenile fiction]]></description>
    <link><![CDATA[https://librivox.org/the-tale-of-bunny-cotton-tail-by-laura-rountree-smith/]]></link>
    <guid><![CDATA[https://librivox.org/the-tale-of-bunny-cotton-tail-by-laura-rountree-smith/]]></guid>
    <pubDate><![CDATA[Fri, 14 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[T. Tembarom by Frances Hodgson Burnett (1849 - 1924)]]></title>
    <description><![CDATA[The story of a Boy living in New York as a street waif, who sells newspapers eventually finds himself to be the heir of an ancient manor. The kids at school never understood what the "T" was for in his name, and he didn't tell them. Does that have something to do in the story? And what about this ancient manor?  Summary by Elijah Fisher]]></description>
    <link><![CDATA[https://librivox.org/t-tembarom-by-frances-hodgson-burnett/]]></link>
    <guid><![CDATA[https://librivox.org/t-tembarom-by-frances-hodgson-burnett/]]></guid>
    <pubDate><![CDATA[Thu, 13 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[Chapters on Evolution by Andrew Wilson (1852 - 1912)]]></title>
    <description><![CDATA[Dr. Andrew Wilson FRSE was a Scottish physiologist and zoologist and lecturer in zoology and comparative anatomy at the University of Edinburgh and  the Royal College of Surgeons of Edinburgh in the late 19th and early 20th century. In the preface of this book, he writes:  "...the chief aim of the work is to present in a popular and readily understood form, the chief evidences of the evolution of living beings. In this view, whilst I have been content to assume the reality of that process, I have also endeavoured to marshal the more prominent facts of zoology and botany, which serve to prove that evolution, broadly considered is not merely a name for an unknown tendency in nature, but is an actual factor in the work of moulding the life with which the universe teems."  - Summary by J. M. Smallheer]]></description>
    <link><![CDATA[https://librivox.org/chapters-on-evolution-by-andrew-wilson/]]></link>
    <guid><![CDATA[https://librivox.org/chapters-on-evolution-by-andrew-wilson/]]></guid>
    <pubDate><![CDATA[Thu, 13 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[Green Valley by Katharine Yirsa Reynolds (1883 - ?)]]></title>
    <description><![CDATA["Traveling men have a poor opinion of it. Ministers of the gospel have been known to despair of it. Socially ambitious matrons move out of it, or, if that is not possible, despise it. Real estate men can not get rich in it. And humorless folk sometimes have a hard, sad time of it in Green Valley". This is an authentic novel about a small town in the USA. A book about what people tell and don't tell, loves and losses, and the importance of community. Community life which did not go away. - Summary by Stav Nisser ]]></description>
    <link><![CDATA[https://librivox.org/green-valley-by-katharine-yirsa-reynolds/]]></link>
    <guid><![CDATA[https://librivox.org/green-valley-by-katharine-yirsa-reynolds/]]></guid>
    <pubDate><![CDATA[Thu, 13 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[Workers - An Experiment in Reality: The West, The by Walter A. Wyckoff (1865 - 1908)]]></title>
    <description><![CDATA[A young scholar, recently graduated from Princeton College, travels across the United States as a member of the working class, taking any job he could find, enduring hardships and struggling to make a living. He travelled mainly on foot, designing for himself a social experiment on experiencing different class and culture structures and the reality of working conditions at the end of the 19th century. This volume continues the story that began in the first volume (<a href="https://librivox.org/the-workers-an-experiment-in-reality-the-east-by-walter-a-wyckoff/">The Workers - An Experiment in Reality -The East</a>), and spans the region from Illinois to California - Summary by Phyllis Vincelli]]></description>
    <link><![CDATA[https://librivox.org/the-workers-an-experiment-in-reality-the-west-by-walter-a-wyckoff/]]></link>
    <guid><![CDATA[https://librivox.org/the-workers-an-experiment-in-reality-the-west-by-walter-a-wyckoff/]]></guid>
    <pubDate><![CDATA[Thu, 13 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[Weltfreund, Der by Franz Werfel (1890 - 1945)]]></title>
    <description><![CDATA[»Mein einziger Wunsch ist, dir, o Mensch, verwandt zu sein!« (aus dem Gedicht "An den Leser")<br>
Dieses könnte als Motto dem Gedichtband vorangestellt sein.<br>
In diesen frühen Gedichten kommt Werfels zutiefst humane Weltsicht zum Ausdruck. Oft auch mit einem leichten Augenzwinkern, wenn es um seine eigene Befindlichkeit geht und immer mit dem Blick auf die kleinen Dinge im Leben, die so wichtig sein können. (Zusammenfassung von seito)]]></description>
    <link><![CDATA[https://librivox.org/der-weltfreund-by-franz-werfel/]]></link>
    <guid><![CDATA[https://librivox.org/der-weltfreund-by-franz-werfel/]]></guid>
    <pubDate><![CDATA[Thu, 13 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[In Quest of El Dorado by Stephen Graham (1884 - 1975)]]></title>
    <description><![CDATA[Lively (and often unsparing) descriptions of the people, places, and customs that the author encounters as he attempts to retrace the steps of the early Spanish conquistadores in the Americas: Columbus, Cortez, Pizarro, Balboa, Coronado. (Summary by Steven Seitel)]]></description>
    <link><![CDATA[https://librivox.org/in-quest-of-el-dorado-by-stephen-graham/]]></link>
    <guid><![CDATA[https://librivox.org/in-quest-of-el-dorado-by-stephen-graham/]]></guid>
    <pubDate><![CDATA[Thu, 13 May 2021 12:55:51 -0400]]></pubDate>
  </item>
     <item>
    <title><![CDATA[Dhammapada (Version 3), The by Unknown]]></title>
    <description><![CDATA[The Dhammapada collects sayings of the Buddha, offering advice on how to live a full and thoughtful life. The translation used for this recording is by Friedrich Max Müller and was first published in the 19th century. - Summary by Newgatenovelist]]></description>
    <link><![CDATA[https://librivox.org/the-dhammapada-by-unknown/]]></link>
    <guid><![CDATA[https://librivox.org/the-dhammapada-by-unknown/]]></guid>
    <pubDate><![CDATA[Wed, 12 May 2021 12:55:51 -0400]]></pubDate>
  </item>
    <!-- end file loop -->
</channel>
</rss>
`
const domParser = new DOMParser();
const doc = domParser.parseFromString(htmlTxt, 'text/html')
for (const item of doc.querySelectorAll("item")){
  console.log(item.querySelector("title").textContent);
}

(RSS feed courtesy of https://librivox.org/pages/librivox-feeds/)

We have the htmlTxt string which has the RSS feed content.

And we create the domParser object with the DOMParser constructor.

Next, we call parseFromString with htmlTxt and 'text/html' to parse the text as HTML.

Then we get all the item noes with doc.querySelectorAll .

And then we call querySelector on item with 'title' to get the title element inside each item element.

The textContent property gets the text content of the title element.

Now we should get:

<![CDATA[Endeavour Journal of Sir Joseph Banks from 25 August 1768-12 July 1771, The by Joseph Banks (1743 - 1820)]]>
<![CDATA[Old New York by Edith Wharton (1862 - 1937)]]>
<![CDATA[Tale of Bunny Cotton-Tail, The by Laura Rountree Smith (1876 - 1924)]]>
<![CDATA[T. Tembarom by Frances Hodgson Burnett (1849 - 1924)]]>
<![CDATA[Chapters on Evolution by Andrew Wilson (1852 - 1912)]]>
<![CDATA[Green Valley by Katharine Yirsa Reynolds (1883 - ?)]]>
<![CDATA[Workers - An Experiment in Reality: The West, The by Walter A. Wyckoff (1865 - 1908)]]>
<![CDATA[Weltfreund, Der by Franz Werfel (1890 - 1945)]]>
<![CDATA[In Quest of El Dorado by Stephen Graham (1884 - 1975)]]>
<![CDATA[Dhammapada (Version 3), The by Unknown]]>

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *