An RSS file describes a set of news items. Here is a sample RSS file:

<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <item>
      <title>Star City</title>
      <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
      <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 Star City.</description>
      <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
    </item>
    
    <item>
      <title>Space Exploration</title>
      <link>http://liftoff.msfc.nasa.gov/</link>
      <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada
        will experience a partial eclipse of the Sun on Saturday, May 31st.</description>
      <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
    </item>
    
    <item>
      <title>The Engine That Does More</title>
      <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
      <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.</description>
      <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
    </item>
  </channel>
</rss>

Note: The RSS format is documented at http://cyber.law.harvard.edu/rss/rss.html.

Your task is to complete the createItem method below. Use the current date (new Date().toString()) for the pubDate.

Complete the following file:

RSSWriter.java

import java.util.Date; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; import org.xml.sax.SAXException; import org.w3c.dom.DOMImplementation; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; public class RSSWriter { /** Builds an RSS document with a single item @param title the title of the item @param link the link of the item @param description the description of the item @return an RSS document with a single item */ public Document build(String title, String link, String description) { doc = builder.newDocument(); Element rss = doc.createElement("rss"); doc.appendChild(rss); rss.setAttribute("version", "2.0"); Element channel = doc.createElement("channel"); rss.appendChild(channel); channel.appendChild(createItem(title, link, description)); return doc; } /** Builds a DOM element for an RSS item. @param title the title @param link the link @param description the description @return a DOM element describing the item */ private Element createItem(String title, String link, String description) { . . . } private Element createTextElement(String name, String text) { Text t = doc.createTextNode(text); Element e = doc.createElement(name); e.appendChild(t); return e; } // this method is used to check your work public String check(String title, String link, String description, String xpathExpr) throws Exception { DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); builder = dbfactory.newDocumentBuilder(); XPathFactory xpfactory = XPathFactory.newInstance(); XPath path = xpfactory.newXPath(); Document doc = build(title, link, description); DOMImplementation impl = doc.getImplementation(); DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0"); LSSerializer ser = implLS.createLSSerializer(); String out = ser.writeToString(doc); // return out; return path.evaluate(xpathExpr, doc); } private DocumentBuilder builder; private Document doc; }