How to Write an RSS Feed Reader in Java

An RSS feed provided by a website is written in XML, so you use Java XML libraries to write an XML feed reader for your desktop. Java includes the necessary XML libraries to read a feed, and you provide a link to the article to read additional content on the RSS feed's website. An RSS reader imports the feed's XML file, parses the title, link and date and displays it in a Java desktop form.

Instructions

    • 1

      Open the Java editor you want to use to create the RSS feed module. Open the project and create a new file in the editor. At the top of the RSS feed source code, add the following necessary XML libraries:

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

    • 2

      Create an instance of the XML classes needed to open the XML file. Add the following code to your reader function:

      XMLInputFactory input = XMLInputFactory.newInstance();
      InputStream stream = read("file.xml");
      XMLEventReader reader = input.createXMLEventReader(stream);

    • 3

      Create a loop that reads through each RSS item. You place the reader code within the loop structure. Copy and paste the following code to create the loop:

      while (reader.hasNext()) {

      }

    • 4

      Display each RSS XML item on the form. The following code displays the title, link and date to the user:

      XMLEvent event = eventReader.nextEvent();
      event.asStartElement().getName().getLocalPart() == ("item")) {
      feed = new Feed(title, link, pubdate);
      event = reader.nextEvent();

Related Searches:

References

Comments

Related Ads

Featured