RSS Reader
Simple command-line based RSS feed reader
import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.CharacterData; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class RSSReader { private static RSSReader instance = null; private RSSReader() { } public static RSSReader getInstance() { if(instance == null) { instance = new RSSReader(); } return instance; } public void getFeeds() { try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); URL u = new URL("http://www.tyrvaansanomat.fi/cs/Satellite?c=AMChannelFeed_C&cid=1194597805463&p=1192554120774&pagename=KOS_newssite%2FAMChannelFeed_C%2FLatest10ArticlesRSS20"); Document doc = builder.parse(u.openStream()); NodeList nodes = doc.getElementsByTagName("item"); for(int i=0;i<nodes.getLength();i++) { Element element = (Element)nodes.item(i); String title = getElementValue(element,"title"); if (title.length() > 43) { title = title.substring(0, 40); title += "..."; } while(title.length() < 43) title += " "; String link = getElementValue(element,"link"); if (link.length() > 43) { link = link.substring(0, 40); link += "..."; } while(link.length() < 43) link += " "; String pubDate = getElementValue(element,"pubDate"); if (pubDate.length() > 43) { pubDate = pubDate.substring(0, 40); pubDate += "..."; } while(pubDate.length() < 43) pubDate += " "; String author = getElementValue(element,"dc:creator"); if (author.length() > 43) { author = author.substring(0, 40); author += "..."; } while(author.length() < 43) author += " "; String comments = getElementValue(element,"wfw:comment"); if (comments.length() > 43) { comments = comments.substring(0, 47); comments += "..."; } while(comments.length() < 43) comments += " "; String description = getElementValue(element,"description"); if (description.length() > 43) { String tmp = description.substring(43); if (tmp.length() > 54) { tmp = tmp.substring(0, 54); tmp += "..."; } while(tmp.length() < 57) tmp += " "; description = description.substring(0, 43); description += "|\n|"; description += tmp; } System.out.println(".---------------------------------------------------------."); System.out.println("|Title: " + title + "|"); System.out.println("|Link: " + link + "|"); System.out.println("|Publish Date: " + pubDate + "|"); System.out.println("|author: " + author + "|"); System.out.println("|comments: " + comments + "|"); System.out.println("|description: " + description + "|"); System.out.println("'---------------------------------------------------------'"); System.out.println(); } } catch(Exception ex) { ex.printStackTrace(); } } private String getCharacterDataFromElement(Element e) { try { Node child = e.getFirstChild(); if(child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } } catch(Exception ex) { } return ""; } protected String getElementValue(Element parent,String label) { return getCharacterDataFromElement((Element)parent.getElementsByTagName(label).item(0)); } public static void main(String[] args) { RSSReader reader = RSSReader.getInstance(); reader.getFeeds(); } }

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Download this code in plain text format here