Tutorial feeds RSS con Java (inglés)

RSS feeds with Java - Tutorial

by Lars Vogel

Version 0.7


05.11.2009

Revision History
Revision 0.1 03.09.2008 Lars Vogel
Created
Revision 0.2 - 1.1 21.06.2011 Lars Vogel
bug fixes and enhancements


RSS Feeds with Java
This tutorial explains how to read and create RSS feeds with
Java. It uses the Stax XML writer which is part of Java
6.0. Eclipse is used as the Java IDE.




1. Overview


1.1. RSS - Really Simple Syndication

An RSS
document is a XML file which can be used to publish blog
entries, news and /or audio and video files. The format of the XML
file is specified via the RSS specification which is currently in
version must be 2.0.

RSS stands for "Really Simple Syndication" (in version 2.0 of
the RSS specification).

1.2. RSS with Java

As an RSS feed is a XML file we can use the Java XML parsers to
read and create
RSS feeds. The following will use the Java Stax XML parser.


For an introduction into XML and its usage with Java see
Java XML Tutorial

2. Project and Domain model

Create the Java project "de.vogella.rss". Create the following
Java classes, which we will use as our
domain model.


package de.vogella.rss;

/*
 * Represents one RSS message
 */

public class FeedMessage {

 String title;
 String description;
 String link;
 String author;
 String guid;

 public String getTitle() {
  return title;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public String getDescription() {
  return description;
 }

 public void setDescription(String description) {
  this.description = description;
 }

 public String getLink() {
  return link;
 }

 public void setLink(String link) {
  this.link = link;
 }

 public String getAuthor() {
  return author;
 }

 public void setAuthor(String author) {
  this.author = author;
 }

 public String getGuid() {
  return guid;
 }

 public void setGuid(String guid) {
  this.guid = guid;
 }

 @Override

 public String toString() {
  return "FeedMessage [title=" + title + ", description=" + description
    + ", link=" + link + ", author=" + author + ", guid=" + guid
    + "]";
 }

}


package de.vogella.rss;

import java.util.ArrayList;
import java.util.List;

/*
 * Stores an RSS feed
 */

public class Feed {

 final String title;
 final String link;
 final String description;
 final String language;
 final String copyright;
 final String pubDate;
 final List<FeedMessage> entries = new ArrayList<FeedMessage>();

 public Feed(String title, String link, String description, String language,
   String copyright, String pubDate) {
  this.title = title;
  this.link = link;
  this.description = description;
  this.language = language;
  this.copyright = copyright;
  this.pubDate = pubDate;
 }

 public List<FeedMessage> getMessages() {
  return entries;
 }

 public String getTitle() {
  return title;
 }

 public String getLink() {
  return link;
 }

 public String getDescription() {
  return description;
 }

 public String getLanguage() {
  return language;
 }

 public String getCopyright() {
  return copyright;
 }

 public String getPubDate() {
  return pubDate;
 }

 @Override

 public String toString() {
  return "Feed [copyright=" + copyright + ", description=" + description
    + ", language=" + language + ", link=" + link + ", pubDate="

    + pubDate + ", title=" + title + "]";
 }

}

3. Read RSS Feeds with Stax


3.1. Create the XML Reader class

Use the Java Project "de.vogella.rss". Create the following
class to read the XML file.



package de.vogella.rss;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;


import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

public class RSSFeedParser {
 static final String TITLE = "title";
 static final String DESCRIPTION = "description";
 static final String CHANNEL = "channel";
 static final String LANGUAGE = "language";
 static final String COPYRIGHT = "copyright";
 static final String LINK = "link";
 static final String AUTHOR = "author";
 static final String ITEM = "item";
 static final String PUB_DATE = "pubDate";
 static final String GUID = "guid";

 final URL url;

 public RSSFeedParser(String feedUrl) {
  try {
   this.url = new URL(feedUrl);
  } catch (MalformedURLException e) {
   throw new RuntimeException(e);
  }
 }

 @SuppressWarnings("null")

 public Feed readFeed() {
  Feed feed = null;
  try {

   boolean isFeedHeader = true;
   // Set header values intial to the empty string
   String description = "";
   String title = "";
   String link = "";
   String language = "";
   String copyright = "";
   String author = "";
   String pubdate = "";
   String guid = "";

   // First create a new XMLInputFactory

   XMLInputFactory inputFactory = XMLInputFactory.newInstance();
   // Setup a new eventReader
   InputStream in = read();
   XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
   // Read the XML document
   while (eventReader.hasNext()) {

    XMLEvent event = eventReader.nextEvent();

    if (event.isStartElement()) {
     if (event.asStartElement().getName().getLocalPart() == (ITEM)) {
      if (isFeedHeader) {
       isFeedHeader = false;
       feed = new Feed(title, link, description, language,
         copyright, pubdate);
      }
      event = eventReader.nextEvent();
      continue;
     }

     if (event.asStartElement().getName().getLocalPart() == (TITLE)) {
      event = eventReader.nextEvent();
      title = event.asCharacters().getData();
      continue;
     }
     if (event.asStartElement().getName().getLocalPart() == (DESCRIPTION)) {
      event = eventReader.nextEvent();
      description = event.asCharacters().getData();
      continue;
     }

     if (event.asStartElement().getName().getLocalPart() == (LINK)) {
      event = eventReader.nextEvent();
      link = event.asCharacters().getData();
      continue;
     }

     if (event.asStartElement().getName().getLocalPart() == (GUID)) {
      event = eventReader.nextEvent();
      guid = event.asCharacters().getData();
      continue;
     }
     if (event.asStartElement().getName().getLocalPart() == (LANGUAGE)) {
      event = eventReader.nextEvent();
      language = event.asCharacters().getData();
      continue;
     }
     if (event.asStartElement().getName().getLocalPart() == (AUTHOR)) {
      event = eventReader.nextEvent();
      author = event.asCharacters().getData();
      continue;
     }
     if (event.asStartElement().getName().getLocalPart() == (PUB_DATE)) {
      event = eventReader.nextEvent();
      pubdate = event.asCharacters().getData();
      continue;
     }
     if (event.asStartElement().getName().getLocalPart() == (COPYRIGHT)) {
      event = eventReader.nextEvent();
      copyright = event.asCharacters().getData();
      continue;
     }
    } else if (event.isEndElement()) {
     if (event.asEndElement().getName().getLocalPart() == (ITEM)) {
      FeedMessage message = new FeedMessage();
      message.setAuthor(author);
      message.setDescription(description);
      message.setGuid(guid);
      message.setLink(link);
      message.setTitle(title);
      feed.getMessages().add(message);
      event = eventReader.nextEvent();
      continue;
     }
    }
   }
  } catch (XMLStreamException e) {
   throw new RuntimeException(e);
  }
  return feed;

 }

 private InputStream read() {
  try {
   return url.openStream();
  } catch (IOException e) {
   throw new RuntimeException(e);
  }
 }
}

3.2. Test the code

The following uses a main method to test. You could also use JUnit

See JUnit Tutorial





package de.vogella.rss;

public class ReadTest {
 public static void main(String[] args) {
  RSSFeedParser parser = new RSSFeedParser(
    "http://www.vogella.de/article.rss");
  Feed feed = parser.readFeed();
  System.out.println(feed);
  for (FeedMessage message : feed.getMessages()) {
   System.out.println(message);

  }

 }
}

4. Write RSS Feeds with Stax


4.1. Create writer

Create the following class to write the XML file.


package de.vogella.rss;

import java.io.FileOutputStream;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;


public class RSSFeedWriter {

 private String outputFile;
 private Feed rssfeed;

 public RSSFeedWriter(Feed rssfeed, String outputFile) {
  this.rssfeed = rssfeed;
  this.outputFile = outputFile;
 }

 public void write() throws Exception {

  // Create a XMLOutputFactory

  XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

  // Create XMLEventWriter
  XMLEventWriter eventWriter = outputFactory
    .createXMLEventWriter(new FileOutputStream(outputFile));

  // Create a EventFactory

  XMLEventFactory eventFactory = XMLEventFactory.newInstance();
  XMLEvent end = eventFactory.createDTD("\n");

  // Create and write Start Tag

  StartDocument startDocument = eventFactory.createStartDocument();

  eventWriter.add(startDocument);

  // Create open tag
  eventWriter.add(end);

  StartElement rssStart = eventFactory.createStartElement("", "", "rss");
  eventWriter.add(rssStart);
  eventWriter.add(eventFactory.createAttribute("version", "2.0"));
  eventWriter.add(end);

  eventWriter.add(eventFactory.createStartElement("", "", "channel"));
  eventWriter.add(end);

  // Write the different nodes

  createNode(eventWriter, "title", rssfeed.getTitle());

  createNode(eventWriter, "link", rssfeed.getLink());

  createNode(eventWriter, "description", rssfeed.getDescription());

  createNode(eventWriter, "language", rssfeed.getLanguage());

  createNode(eventWriter, "copyright", rssfeed.getCopyright());

  createNode(eventWriter, "pubdate", rssfeed.getPubDate());

  for (FeedMessage entry : rssfeed.getMessages()) {
   eventWriter.add(eventFactory.createStartElement("", "", "item"));
   eventWriter.add(end);
   createNode(eventWriter, "title", entry.getTitle());
   createNode(eventWriter, "description", entry.getDescription());
   createNode(eventWriter, "link", entry.getLink());
   createNode(eventWriter, "author", entry.getAuthor());
   createNode(eventWriter, "guid", entry.getGuid());
   eventWriter.add(end);
   eventWriter.add(eventFactory.createEndElement("", "", "item"));
   eventWriter.add(end);

  }

  eventWriter.add(end);
  eventWriter.add(eventFactory.createEndElement("", "", "channel"));
  eventWriter.add(end);
  eventWriter.add(eventFactory.createEndElement("", "", "rss"));

  eventWriter.add(end);

  eventWriter.add(eventFactory.createEndDocument());

  eventWriter.close();
 }

 private void createNode(XMLEventWriter eventWriter, String name,

 String value) throws XMLStreamException {
  XMLEventFactory eventFactory = XMLEventFactory.newInstance();
  XMLEvent end = eventFactory.createDTD("\n");
  XMLEvent tab = eventFactory.createDTD("\t");
  // Create Start node

  StartElement sElement = eventFactory.createStartElement("", "", name);
  eventWriter.add(tab);
  eventWriter.add(sElement);
  // Create Content
  Characters characters = eventFactory.createCharacters(value);
  eventWriter.add(characters);
  // Create End node
  EndElement eElement = eventFactory.createEndElement("", "", name);
  eventWriter.add(eElement);
  eventWriter.add(end);
 }
}

   


4.2. Test the code

The following uses a main method to test. You could also use JUnit.
See JUnit Tutorial


Run the program to write the RSS file "articles.rss".


package de.vogella.rss;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import java.util.Locale;

public class WriteTest {

 public static void main(String[] args) {
  // Create the rss feed

  String copyright = "Copyright hold by Lars Vogel";
  String title = "Eclipse and Java Information";
  String description = "Eclipse and Java Information";
  String language = "en";
  String link = "http://www.vogella.de";
  Calendar cal = new GregorianCalendar();
  Date creationDate = cal.getTime();
  SimpleDateFormat date_format = new SimpleDateFormat(
    "EEE', 'dd' 'MMM' 'yyyy' 'HH:mm:ss' 'Z", Locale.US);
  String pubdate = date_format.format(creationDate);
  Feed rssFeeder = new Feed(title, link, description, language,
    copyright, pubdate);

  // Now add one example entry

  FeedMessage feed = new FeedMessage();
  feed.setTitle("RSSFeed");
  feed.setDescription("This is a description");
  feed.setAuthor("nonsense@somewhere.de (Lars Vogel)");
  feed.setGuid("http://www.vogella.de/articles/RSSFeed/article.html");
  feed.setLink("http://www.vogella.de/articles/RSSFeed/article.html");
  rssFeeder.getMessages().add(feed);

  // Now write the file

  RSSFeedWriter writer = new RSSFeedWriter(rssFeeder, "articles.rss");
  try {
   writer.write();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

5. Thank you


Please help me to support this article:


Flattr this








6. Questions and Discussion

Before posting questions, please see the
vogella FAQ
. If you have questions or find an error in this article please
use the
www.vogella.de Google Group

. I have created a short list
how to create good questions
which might also help you.

7. Links and Literature

http://feedvalidator.org/


Webpage which validates feeds

Comentarios