Can you find the bug in <div> I do NOT get sample xml data[bookrss.xml]... RSS Client

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Leonidas Savvides
    New Member
    • Aug 2010
    • 17

    Can you find the bug in <div> I do NOT get sample xml data[bookrss.xml]... RSS Client

    Can you find the bug in <div> I do NOT get sample xml data[bookrss.xml]... RSS Client/Server?
    I get
    xhrequest.ready State == 4 && xhrequest.statu s == 200 ... 4 & 200 , but no data (local host run)
    is this correct
    1)var titles = xhrequest.respo nseXML.getEleme ntsByTagName('t itle');
    titles[0].firstChild.nod eValue+"</h1>";
    2)xml file same folder as .java file in NETBEANS
    client
    Code:
      <%-- 
        Document   : index
        Created on : Jul 22, 2010, 11:32:20 AM
        Author     : Shop
    --%>
    
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
            
            
      <script type="text/javascript">
    // getxmlhttprequest.js
    function getXMLHttpRequest()
    {
     var xhrequest = null;
     if(window.XMLHttpRequest)
     {
     // If IE7, Mozilla, Safari, etc: Use native object
      try
      {
       xhrequest = new XMLHttpRequest();
       return xhrequest;
      }
      catch(exception)
      {
      // OK, just carry on looking
      }
     }
     else
     {
    
      // ...otherwise, use the ActiveX control for IE5.x and IE6
       var IEControls = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"];
       for(var i=0; i<IEControls.length; i++)
       {
        try
        {
         xhrequest = new ActiveXObject(IEControls[i]);
         return xhrequest;
        }
        catch(exception)
        {
         // OK, just carry on looking
        }
       }
      // if we got here we didn�t find and matches
      throw new Error("Cannot create an XMLHttpRequest");
     }
    }
    
    //processnewsfeed.js
    function processRSSFeed()
    {
          alert("alert processRSSFeed() "+ xhrequest.readyState + " " + xhrequest.status);
     if(xhrequest.readyState == 4 && xhrequest.status == 200)
     {
           alert("alert readyState/status success processRSSFeed()");
      var titles = xhrequest.responseXML.getElementsByTagName('title');
      var links = xhrequest.responseXML.getElementsByTagName('link');
      var descriptions = xhrequest.responseXML.getElementsByTagName('description');
      var firstItemTitle = "<h1>"+titles[0].firstChild.nodeValue+"></h1>";
      var firstItemLink = "<h3>"+links[0].firstChild.nodeValue+"</h3>";
      var firstItemDescription = "<h3>"+descriptions[0].firstChild.nodeValue+"</h3>";
      
      intro = firstItemTitle+firstItemLink+firstItemDescription;         
      //alert(links[0].firstChild.nodeValue);
    for (var i=1; i<=3; i++)            
    {
      var firstItemTitle2 = "<p>"+"<h2>"+titles[i].firstChild.nodeValue+"</h2>";
      var firstItemLink2 = "<h3>"+links[i].firstChild.nodeValue+"</h3>";
      var firstItemDescription2 = "<h3>"+descriptions[i].firstChild.nodeValue+"</h3>"+"</p>";   
      total += firstItemTitle2+firstItemLink2+firstItemDescription2+"";
      
    }            
                   
      document.getElementById("feed").innerHTML=intro+total;
     }
    }      
          
          
    function ajaxGetRSS()      
    {      
      // no 'var', so this is a global variable!
      alert("alert ajaxGetRSS()");
        xhrequest = null;
        try
        {
         xhrequest = getXMLHttpRequest();
        }
        catch(error)
        {
         document.write("Cannot run Ajax code using this browser");
        }
        if(xhrequest != null)
        {
          xhrequest.onreadystatechange = processRSSFeed;
    	  xhrequest.open("POST", "http://localhost:8084/ParsonsWebServicesAjax/rssfeed", true);
          xhrequest.send(null);
        }
    }
    
    </script>        
        </head>
        <body>
        <h2>Hello World!</h2>
        <br /><form action="" onsubmit="ajaxGetRSS();">
        <input type="submit" value="Press Appear Data" name="getData" />
        <br /></form>
        <a >click here</a>
        <p>&nbsp;</p>
        <div id="feed"></div>
        </body>
    </html>
    server
    Code:
      /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    
    import java.io.*;
    import java.net.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /**
     *
     * @author Shop
     */
    public class AjaxRSSServlet extends HttpServlet {
       
        /** 
        * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
        * @param request servlet request
        * @param response servlet response
        */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            
            PrintWriter out = null;
            try {
            response.setContentType("text/xml");
            out = response.getWriter();
    
            File xmlfile = new File("bookrss.xml");
            
            StringBuffer sb = new StringBuffer();
            BufferedReader br = new BufferedReader(new FileReader(xmlfile));
            String s = br.readLine();
            while (s != null)
            {
                sb.append(s.trim());
                s=br.readLine();
                
            }
                    System.out.println(sb);
                    out.print(sb);
            } catch(IOException e) { 
                e.printStackTrace();
                //return null;
            }
            //return null;
        } 
    
    
    }
    
    bookrss.xml   // version is correct?
    
    <rss version="0.91"> 
    <channel> 
    <title>hT</title> 
    <link>hL</link> 
    <description>hD</description> 
    <item> 
    <title>hT1</title> 
    <link>hL1</link> 
    <description>hD1</description> 
    </item> 
    <item> 
    <title>hT2</title> 
    <link>hL2</link> 
    <description>hD2</description> 
    </item> 
    <item> 
    <title>hT3</title> 
    <link>hL3</link> 
    <description>hD3</description> 
    </item> 
    </channel> 
    </rss>
Working...