title: Help building RSS reader in Java
body: <p>For a class project, I'm trying to write a  simple RSS reader for my Java class.  I am trying to walk the DOM tree just to get the experience doing it although I know there are better more efficient ways and tools.  I have a ReaderObject that gets the basic title, link, description, and a List to hold RSSItem objects that have the instance variables title, link, description, publication date, and guid.  I was hoping with this information, I could parse and redisplay it in a nice way.  I'm getting stuck with the RSSItem part since my text is blank there.  I also don't know if this is an ok way to do it, and if I understand it fully...  </p>
  
  <p>Another question is when you getChildNodes, and then with a for loop, you get each item, why do I need to getFirstChild at this point.  I got that from the book example, but I do not know why.  </p>
  
  <p>Here is my code: </p>
  
  <pre><code>Code: 
  import java.io.*;
  import java.util.*;
  import javax.xml.parsers.*;
  import org.w3c.dom.*;
  import org.xml.sax.*;
  
  
  public class RSSReader {
      public static void main(String[] args) {
          File f = new File("testrss.xml");
          if (f.isFile()) {
              System.out.println("is File");
              RSSReader xml = new RSSReader(f);
          }
      }
  
      public RSSReader(File xmlFile) {
          try {
              obj = new ReaderObject();
  
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              DocumentBuilder builder = factory.newDocumentBuilder();
              Document doc = builder.parse(xmlFile); // Document extends Node
  
              List&lt;Node&gt; nodeList = new ArrayList&lt;Node&gt;();
              nodeList.add(doc);
  
              while (nodeList.size() &gt; 0)
              {
              Node node = nodeList.get(0);
  
  //            if(node instanceof Document)
  //                 System.out.println("Document Node");
  
              // Get entries in the xml file
              if (node.hasChildNodes()) {
                  NodeList nl = node.getChildNodes();
                  for(int i = 0; i &lt; nl.getLength(); i++) {
                      if (nl.item(i) instanceof Element) {
                          Element childElement = (Element) nl.item(i);
                          nodeList.add(childElement);
                          //nodeList.add(nl.item(i));
                      }
                  }
              }
  
              if (node instanceof Element) {
                  // Print out the element tag name
                  System.out.println("Element Node: " + ((Element)node).getTagName());
  
                  // Print out the attributes of the element
                  if (node.hasAttributes()) {
                      NamedNodeMap attrMap = node.getAttributes();
                      for (int i = 0; i &lt; attrMap.getLength(); i++) {
                          Attr attribute = (Attr) attrMap.item(i);
                          System.out.print("\tAttribute Key: " + attribute.getName() + " Value: " + attribute.getValue());
                      }
                      System.out.println();
                  }
  
                  // Get children of node
                  if (node.hasChildNodes()) {
                      NodeList childrenList = node.getChildNodes();
                      for (int j = 0; j &lt; childrenList.getLength(); j++) {
                          Node child = childrenList.item(j);
                          Element childElement;
                          Text textNode;
                          if (child instanceof Element) {
                              childElement = (Element) child;
                              textNode = (Text) childElement.getFirstChild();
                              String text = textNode.getData().trim();
                              if (childElement.getTagName().toLowerCase().equals("title")) {
  
                                  obj.setTitle(text);
                                  System.out.println("Title: " + obj.getTitle());
                              }
                              else if (childElement.getTagName().toLowerCase().equals("link")) {
                                  obj.setLink(text);
                                  System.out.println("Link: " + obj.getLink());
                              }
                              else if (childElement.getTagName().toLowerCase().equals("description")) {
                                  obj.setDescription(text);
                                  System.out.println("Description: " + obj.getDescription());
                              }
                              else if (childElement.getTagName().toLowerCase().equals("item")) {
                                  RSSItem item = new RSSItem();
                                  System.out.println("item text: " + text); // STUCK HERE
                                  item.setTitle(text);
                                  System.out.println("RSS Item title: " + item.getTitle());
                              }
                          }
                      }
                  }
              }
  
              nodeList.remove(0);
              }
          }
          catch (IOException e) {
              e.printStackTrace();
          }
          catch (SAXException e) {
              e.printStackTrace();
          }
          catch (IllegalArgumentException e) {
              e.printStackTrace();
          }
          catch (ParserConfigurationException e) {
              e.printStackTrace();
          }
      }
      private ReaderObject obj;
  }
  class ReaderObject {
      public ReaderObject() {
          this.title = "";
          this.link = "";
          this.description = "";
      }    
  
      public ReaderObject(String title, String link, String description) {
  
          this.title = title;
          this.link = link;
          this.description = description;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public void setLink(String link) {
          this.link = link;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public String getTitle() {
          return title;
      }
  
      public String getLink() {
          return link;
      }
  
      public String getDescription() {
          return description;
      }
  
      private String title;
      private String link;
      private String description;
      private List&lt;RSSItem&gt; items = new ArrayList&lt;RSSItem&gt;();
  }
  
  class RSSItem {
  
      public RSSItem() {
  
          this.title = "";
          this.link = "";
          this.description = "";
          this.pubDate = "";
          this.guid = "";
      }    
  
      public RSSItem(String title, String link, String description, String item, String pubDate, String guid) {
  
          this.title = title;
          this.link = link;
          this.description = description;
          this.pubDate = pubDate;
          this.guid = guid;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public void setLink(String link) {
          this.link = link;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public void setPubDate(String pubDate) {
          this.pubDate = pubDate;
      }
  
      public void setGuid(String guid) {
          this.guid = guid;
      }
  
      public String getTitle() {
          return title;
      }
      private String title;
      private String link;
      private String description;
      private String pubDate;
      private String guid;
  }
  
  Output:
  is File
  Element Node: rss
      Attribute Key: version Value: 2.0
  Element Node: channel
  Title: Liftoff News
  Link: http://liftoff.msfc.nasa.gov/
  Description: Liftoff to Space Exploration.
  item text: 
  RSS Item title: 
  item text: 
  RSS Item title: 
  item text: 
  RSS Item title: 
  item text: 
  RSS Item title: 
  Element Node: title
  Element Node: link
  Element Node: description
  Element Node: language
  Element Node: pubDate
  Element Node: lastBuildDate
  Element Node: docs
  Element Node: generator
  Element Node: managingEditor
  Element Node: webMaster
  Element Node: item
  Title: Star City
  Link: http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp
  Description: How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &lt;a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm"&gt;Star City&lt;/a&gt;.
  Element Node: item
  Description: Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &lt;a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm"&gt;partial eclipse of the Sun&lt;/a&gt; on Saturday, May 31st.
  Element Node: item
  Title: The Engine That Does More
  Link: http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp
  Description: Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.
  Element Node: item
  Title: Astronauts' Dirty Laundry
  Link: http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp
  Description: Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.
  Element Node: title
  Element Node: link
  Element Node: description
  Element Node: pubDate
  Element Node: guid
  Element Node: description
  Element Node: pubDate
  Element Node: guid
  Element Node: title
  Element Node: link
  Element Node: description
  Element Node: pubDate
  Element Node: guid
  Element Node: title
  Element Node: link
  Element Node: description
  Element Node: pubDate
  Element Node: guid
  
  XML Code:
      &lt;?xml version="1.0"?&gt; 
      &lt;rss version="2.0"&gt; 
      &lt;channel&gt; 
      &lt;title&gt;Liftoff News&lt;/title&gt; 
      &lt;link&gt;http://liftoff.msfc.nasa.gov/&lt;/link&gt; 
      &lt;description&gt;Liftoff to Space Exploration.&lt;/description&gt; 
      &lt;language&gt;en-us&lt;/language&gt; 
      &lt;pubDate&gt;Tue, 10 Jun 2003 04:00:00 GMT&lt;/pubDate&gt;
       &lt;lastBuildDate&gt;Tue, 10 Jun 2003 09:41:01 GMT&lt;/lastBuildDate&gt; 
       &lt;docs&gt;http://blogs.law.harvard.edu/tech/rss&lt;/docs&gt; 
       &lt;generator&gt;Weblog Editor 2.0&lt;/generator&gt; 
       &lt;managingEditor&gt;editor@example.com&lt;/managingEditor&gt;
        &lt;webMaster&gt;webmaster@example.com&lt;/webMaster&gt;
         &lt;item&gt; 
         &lt;title&gt;Star City&lt;/title&gt; 
         &lt;link&gt;http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp&lt;/link&gt; 
         &lt;description&gt;How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's &amp;lt;a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm"&amp;gt;Star City&amp;lt;/a&amp;gt;.&lt;/description&gt;
          &lt;pubDate&gt;Tue, 03 Jun 2003 09:39:21 GMT&lt;/pubDate&gt; 
          &lt;guid&gt;http://liftoff.msfc.nasa.gov/2003/06/03.html#item573&lt;/guid&gt;
           &lt;/item&gt; 
           &lt;item&gt; 
           &lt;description&gt;Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a &amp;lt;a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm"&amp;gt;partial eclipse of the Sun&amp;lt;/a&amp;gt; on Saturday, May 31st.&lt;/description&gt;
            &lt;pubDate&gt;Fri, 30 May 2003 11:06:42 GMT&lt;/pubDate&gt; 
            &lt;guid&gt;http://liftoff.msfc.nasa.gov/2003/05/30.html#item572&lt;/guid&gt;
             &lt;/item&gt; &lt;item&gt; &lt;title&gt;The Engine That Does More&lt;/title&gt; 
             &lt;link&gt;http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp&lt;/link&gt; 
             &lt;description&gt;Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.&lt;/description&gt;
              &lt;pubDate&gt;Tue, 27 May 2003 08:37:32 GMT&lt;/pubDate&gt; 
              &lt;guid&gt;http://liftoff.msfc.nasa.gov/2003/05/27.html#item571&lt;/guid&gt; 
              &lt;/item&gt; &lt;item&gt; &lt;title&gt;Astronauts' Dirty Laundry&lt;/title&gt;
               &lt;link&gt;http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp&lt;/link&gt; 
               &lt;description&gt;Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.&lt;/description&gt; &lt;pubDate&gt;Tue, 20 May 2003 08:56:02 GMT&lt;/pubDate&gt; 
               &lt;guid&gt;http://liftoff.msfc.nasa.gov/2003/05/20.html#item570&lt;/guid&gt; 
               &lt;/item&gt;
                &lt;/channel&gt; 
                &lt;/rss&gt;
  </code></pre>