Reading RSS in Java

In the previous article, I talked about XML.  It is a hierarchical structure of which each node is represented by <tag> enclosed tags.  The closing tag can either be </tag> or <tag /> format if there is no tags in it.

RSS is a type of XML format that consists of pre-defined tags.  Here is how you read RSS feeds.  The structure is simple: the top node is "channel" and the articles are under "item" nodes.  Each "item" node contains nodes such as "title", "description", "link", and so on.

The RSS reading code therefore is the same as in the way you read XML files.  I created a library called com.github.easai.text that can simplify the process pretty much.  Using the library, the procedure is abstracted and encapsulated.  So load:

 
        <dependency>
            <groupId>com.github.easai</groupId>
            <artifactId>text</artifactId>
            <version>0.1.5</version>
        </dependency>


Call the function RSS#getList() as follows.  Supply the URL for the RSS.  The return value is an ArrayList of ArrayList of Node's.  By going through each, you can get the text contents of each "item" or the article.  Here the code emits "title", "description", and "link" elements of the feeds.

 
    public void readRSS(String rss) {
        ArrayList<ArrayList<Node>> itemList = RSS.getList(rss);
        
        for (ArrayList<Node> list : itemList) {
            for (Node ele: list){
                if (ele.getNodeName().equals("title") || ele.getNodeName().equals("description")
                        || ele.getNodeName().equals("link")) {
                    System.out.println(ele.getTextContent());
                }
            }
        }
        System.out.println();
    }

Comments

Popular posts from this blog

Logging your Maven project with Logback

TreeEditor: Customizing Drag and Drop

Spring Tool Suite