FileDocCategorySizeDatePackage
GetRSSChannelServlet.javaAPI DocExample6808Sat Jun 03 18:32:50 BST 2000com.techbooks

GetRSSChannelServlet

public class GetRSSChannelServlet extends HttpServlet

Fields Summary
private static final String
hostname
Host to connect to for books list
private static final int
portNumber
Port number to connect to for books list
private static final String
file
File to request (URI path) for books list
Constructors Summary
Methods Summary
private java.lang.StringgenerateRSSContent(org.jdom.Document doc)

This will generate an RSS XML document using the supplied JDOM Document.
param
doc Document to use for input.
return
String - RSS file to output.
throws
JDOMException when errors occur.

        StringBuffer rss = new StringBuffer();
        
        rss.append("<?xml version=\"1.0\"?>\n")
           .append("<!DOCTYPE rss PUBLIC ")
           .append("\"-//Netscape Communications//DTD RSS 0.91//EN\" ")
           .append("\"http://my.netscape.com/publish/formats/rss-0.91.dtd\">\n")
           .append("<rss version=\"0.91\">\n")
           .append(" <channel>\n")
           .append("  <title>Technical Books</title>\n")
           .append("  <link>http://newInstance.com/javaxml/techbooks</link>\n")
           .append("  <description>\n")
           .append("   Your online source for technical materials, computers, ")
           .append("and computing books!\n")
           .append("  </description>\n")
           .append("  <language>en-us</language>\n")
           .append("  <image>\n")
           .append("   <title>mytechBooks.com</title>\n")
           .append("   <url>")
           .append("http://newInstance.com/javaxml/techbooks/images/techbooksLogo.gif")
           .append("</url>\n")
           .append("   <link>http://newInstance.com/javaxml/techbooks</link>\n")
           .append("   <width>140</width>\n")
           .append("   <height>23</height>\n")
           .append("   <description>\n")
           .append("    Your source on the Web for technical books.\n")
           .append("   </description>\n")
           .append("  </image>\n");
           
        // Add an item for each new title with Computers as subject
        List books = doc.getRootElement().getChildren("book");
        for (Iterator i = books.iterator(); i.hasNext(); ) {
            Element book = (Element)i.next();
            if (book.getAttribute("subject")
                    .getValue().equals("Computers")) {
                        
                // Output an item
                rss.append("<item>\n")
                    // Add title
                   .append(" <title>")
                   .append(book.getChild("title").getContent())
                   .append("</title>\n")
                    // Add link to buy book
                   .append(" <link>")
                   .append("http://newInstance.com/javaxml/techbooks/buy.xsp?isbn=")
                   .append(book.getChild("saleDetails")
                               .getChild("isbn")
                               .getContent())
                   .append("</link>\n")
                   .append(" <description>")
                    // Add description
                   .append(book.getChild("description").getContent())
                   .append("</description>\n")                       
                   .append("</item>\n");
                        
            }
        }          
         
        rss.append(" </channel>\n")
           .append("</rss>");
        
        return rss.toString();
    
public voidservice(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


          
                       
            
        res.setContentType("text/plain");
        PrintWriter out = res.getWriter();
        
        // Connect and get XML listing of books
        URL getBooksURL = new URL("http", hostname, portNumber, file);
        InputStream in = getBooksURL.openStream();

        try {
            // Request SAX Implementation and use default parser
            Builder builder = new SAXBuilder();

            // Create the document
            Document doc = builder.build(in);
            
            // Output XML
            out.println(generateRSSContent(doc));
            
        } catch (JDOMException e) {        
            out.println("Error: " + e.getMessage());
        } finally {
            out.close();
        }