Java

 

Introduction

RSS (Really Simple Syndication) is a standard format used for delivering regularly changing web content such as news articles, blog posts, or podcasts. In this tutorial, we will learn how to create a simple RSS feed reader using Java. By the end of this tutorial, you’ll be able to fetch and display RSS feed data from a given URL, making it easy to stay updated with the latest content from your favorite websites.

Objective

The objective of this tutorial is to help you understand how to:

  • Parse an RSS feed using Java.
  • Fetch the feed from a URL.
  • Display the RSS feed items such as title, link, and description.

Code for RSS Feed Reader in Java

import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

public class RSSFeedReader {

    public static void main(String[] args) {
        try {
            // The URL of the RSS feed
            String rssUrl = "https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml"; // Example URL (New York Times RSS feed)

            // Create a URL object from the feed URL
            URL url = new URL(rssUrl);

            // Create a DocumentBuilderFactory instance
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

            // Create a DocumentBuilder instance
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

            // Parse the RSS feed and get the Document object
            Document doc = dBuilder.parse(url.openStream());

            // Normalize the XML structure
            doc.getDocumentElement().normalize();

            // Get all  nodes (RSS feed entries)
            NodeList nList = doc.getElementsByTagName("item");

            // Iterate over each item and print out the details
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    // Extract and print title, link, and description
                    String title = eElement.getElementsByTagName("title").item(0).getTextContent();
                    String link = eElement.getElementsByTagName("link").item(0).getTextContent();
                    String description = eElement.getElementsByTagName("description").item(0).getTextContent();

                    // Output the details of each RSS item
                    System.out.println("Title: " + title);
                    System.out.println("Link: " + link);
                    System.out.println("Description: " + description);
                    System.out.println("-----------");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Explanation of the Program

In this program, we are using Java’s javax.xml.parsers library to parse the RSS feed, which is in XML format. The main steps in the program are as follows:

  • URL Setup: The URL of the RSS feed is defined in the rssUrl variable. You can replace this URL with any RSS feed URL you prefer.
  • Document Parsing: We use DocumentBuilderFactory and DocumentBuilder to parse the RSS feed into a Document object, which represents the XML structure of the feed.
  • Normalization: The normalize() method is called to clean up the document structure, ensuring it’s well-formed.
  • Extracting Items: The getElementsByTagName("item") method retrieves all the RSS feed entries, and we loop through each entry to extract and print the title, link, and description.

How to Run the Program

  1. Set up your Java development environment: Ensure you have JDK installed and a Java IDE (like IntelliJ IDEA or Eclipse) or a text editor (like VS Code) to write your Java code.
  2. Create a new Java project: Create a new Java project and a new Java class named RSSFeedReader.
  3. Copy and paste the code: Copy the code provided above and paste it into the newly created class.
  4. Run the program: Execute the program from your IDE or command line. If you’re using the command line, compile and run the program using the following commands:
    javac RSSFeedReader.java
    java RSSFeedReader
    
  5. View the output: The program will fetch the RSS feed and display the title, link, and description of each feed item in the console.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)