• Como Publicar RSS Usando Java

    RSS es una de las ideas mas simples y mas exitosas de los ultimos tiempos. Es simplemente una manera de compartir informacion de una manera standard.

    RSS significa Really Simple Sindication y no es nada mas que un formato XML standard el cual muchisimos blogs y sitios de noticias usan para publicar sus noticias asi los newreaders (como netvibes o igoogle) pueden prooveer a sus usuarios de una manera humana para leer esos feeds.

    En este articulo vamos a ver como usando la API ROME se puede escribir RSS con solo un par de lineas de codigo.

    Primero que nada van a tener que bajar 2 archivos JAR e incluirlos en tus libraries.

    A continuacion la clase completa del RSSWriter y debajo el uso.

    import java.io.Writer;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import com.sun.syndication.feed.synd.SyndContentImpl;
    import com.sun.syndication.feed.synd.SyndEntryImpl;
    import com.sun.syndication.feed.synd.SyndFeed;
    import com.sun.syndication.feed.synd.SyndFeedImpl;
    import com.sun.syndication.io.SyndFeedOutput;
    
    /**
     * @author: Rodrigo Asensio - rasensio@gmail.com
     */
    public class RSSWriter {
    
    SyndFeed feed = new SyndFeedImpl();
    
    public RSSWriter() {
    	this.feed.setFeedType("rss_2.0");
    }
    
    public void setTitle(String title) {
    	this.feed.setTitle(title);
    }
    
    public void setType(String type) {
    	this.feed.setFeedType(type);
    }
    
    public void setLink(String link) {
    	this.feed.setLink(link);
    }
    
    public void setDescription(String description) {
    	this.feed.setDescription(description);
    }
    
    public void addEntry(String title, String link, Date date, String text) {
    	SyndEntryImpl entry = new SyndEntryImpl();
    	entry.setTitle(title);
    	entry.setLink(link);
    	entry.setPublishedDate(date);
    	SyndContentImpl description = new SyndContentImpl();
    	description.setType("text/plain");
    	description.setValue(text);
    	entry.setDescription(description);
    	List entries = this.feed.getEntries();
    	if (entries == null) {
    		entries = new ArrayList();
    		this.feed.setEntries(entries);
    	}
    	entries.add(entry);
    }
    
    public void serialize(Writer writer) throws Exception {
    	SyndFeedOutput output = new SyndFeedOutput();
    	output.output(this.feed, writer);
    }
    
    public String serialize() throws Exception {
    	SyndFeedOutput output = new SyndFeedOutput();
    	return output.outputString(this.feed);
    }
    

    El uso basico para esta clase seria….

      RSSWriter writer = new RSSWriter();
      writer.setTitle("Some title");
      writer.setDescription("Some description");
      writer.setLink("http://yourdomain.com");
      writer.addEntry("news 1", "http://yourdomain.com/news1", new Date(), "some text");
      // write to xml
      writer.serialize(new PrintWriter("/var/www/mysite/rss.xml"));
    

Leave a comment