Read RSS feeds by Using Java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author vijay
*
*/
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 writeNews() {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL(“http://pratyush.in/feed/rss”); // your feed url
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);
System.out.println(“Title: ” + getElementValue(element,”title”));
System.out.println(“Link: ” + getElementValue(element,”link”));
System.out.println(“Publish Date: ” + getElementValue(element,”pubDate”));
System.out.println(“author: ” + getElementValue(element,”dc:creator”));
System.out.println(“comments: ” + getElementValue(element,”wfw:comment”));
System.out.println(“description: ” + getElementValue(element,”description”));
System.out.println();
}//for
}//try
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 “”;
} //private String getCharacterDataFromElement
protected float getFloat(String value) {
if(value != null && !value.equals(“”)) {
return Float.parseFloat(value);
}
return 0;
}
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.writeNews();
}
}
Note: this is not my program, i copied from java forums, after success full running i feel that it may help for my blog reader. i don`t have any rights on this program.
you can find original post @ http://forums.sun.com/thread.jspa?threadID=5275485


you can find more and clear explanation @ http://www.sitepoint.com/article/rss-feeds-jsp-based-web-apps/
by mr Simon Brown
katta vijay
June 5, 2009 at 5:22 pm
thanks for the tip
tiberiumihai
October 2, 2010 at 3:35 am
~
There are some glaring bugs and easy, possible optimizations in your code example. I would code the same functionality of getFloat like this:
~
protected float getFloat(String value) {
float fVal= (float).0;
if(value != null){
value = value.trim();
if(value.length() > 0){ fVal = Float.parseFloat(value); }
}
return fVal;
}
~
!value.equals(“”) does not catch the case in which you would pass a string like ” ” as value. You must trim the string and then you can simply call .length() on the actual String object
~
Also I would change also the calling context to do those obvious syntactic tests (and probably handable) before the function call and there should be only one entry and exit in your code …
~
hsymbolicus
hsymbolicus
November 25, 2010 at 10:49 pm