Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet read XML file and display it using java servlets
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


madhuri
read XML file and display it using java servlets
1 Answer(s)      3 years and 5 months ago
Posted in : JSP-Servlet


sir, i can't access Xml which is present in my d drive plz can u should go through my code n tell me the things where i went wrong

java servlet program

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException 
{
    response.setContentType("text/xml");

    FileRead fr = new FileRead();
    FileWrite fw = new FileWrite();


    ResultSet rs=null,rs_path=null;

    String xmlpath;
    response.setHeader("Cache-Control", "no-cache");
    System.out.println("Hai "+request.getParameter("url"));
    String url = request.getParameter("url");
    String userName = request.getParameter("userName");
    String searchPassword = request.getParameter("searchPass");
    System.out.println(userName+" "+searchPassword);
    try
    {

        PrintWriter out = response.getWriter();
       rs = search_pass.execSQL("Select * from Member_Registration where searchpass='"+searchPassword+"' and username='"+userName+"' and FaultSearch=0");

    }if(rs.next())
        {

            tempValue=0;
            rs = bean_obj.execSQL("Select * from feed_url where URL='"+url+"' and username='"+userName+"'");
            System.out.println(rs.next());
            String filename= request.getParameter("FileName");
           filename = filename.replace(":","_");
            xmlpath = "D:/Leo/"+filename+".xml";
            out.println("<p Style='position:Absolute;top:275px;left:660px;font:bold 12pt/14pt Times New Roman;color:#FFFAF0'>"+xmlpath+"</p>");
            if(rs.next())
            {

                rs_path = bean_obj1.execSQL("Select filename from feed_url where url='"+url+"' and username='"+userName+"'");
                rs_path.next();
                String ss =filename; 
                rs_path.getString("FileName");
                System.out.println(ss);
                xmlpath = "D:/Leo/"+ss+".xml";
                String fileContent = fr.fileRead(xmlpath);
                System.out.println(fileContent);
                out.println(fileContent);
            }   
            else
            {
                out.println("New");

                bean_obj.updateSQL("insert into feed_URL(userName,URL,FileName) values('"+userName+"','"+url+"','"+filename+"')");

                readXML = new XmlBean(userName,url); 
                String str = fw.fileWrite(xmlpath,readXML);

system.out.println(str);

            }
            search_update.updateSQL("UPDATE Member_Registration SET FaultSearch=0 WHERE Username='"+userName+"'");
        }

            //System.out.println("Invalid s1234earch Password "+tempValue);
      else
        {
            if(++tempValue>=3)

            {
                search_update.updateSQL("UPDATE Member_Registration SET FaultSearch=1 WHERE Username='"+userName+"'");
                tempValue=0;
                response.sendRedirect("Search_Password_Error_logout.jsp");
                int i=00;
                out.println("Lock");
            }
            else
            {
                out.println("Invalid search Password "+tempValue);
                response.sendRedirect("Search_Password_Error_logout.jsp");
                int i=0;
                out.println("Invalid");
            }

    }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
 * @param request servlet request
 * @param response servlet response
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

/** Handles the HTTP <code>POST</code> method.
 * @param request servlet request
 * @param response servlet response
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

/** Returns a short description of the servlet.
 */
public String getServletInfo() {
    return "Short description";
}
// </editor-fold>

}

jsp:

<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%-- The taglib directive below imports the JSTL library. If you uncomment it, you must also add the JSTL library to the project. The Add Library... action on Libraries node in Projects view can be used to add the JSTL 1.1 library. --%> <%-- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> --%>

Online RSS Reader

    <%
        String usname = (String)session.getAttribute("uname");
        session.setMaxInactiveInterval(-1);
        //out.println(usname);
    %>
    <p  style="position:absolute;font : bold 12pt/14pt Times New Roman;top: 8px;left:650px;">
      <%  
        String name = (String)session.getAttribute("MemberName");
        out.println(name);
        session.setMaxInactiveInterval(-1);
       %>
    </p>
  <body>  
      <form id="main">
    <!--  DIV FOR LOG OUT  -->
    <div style="position:absolute;top:10px;left:800px;cursor:hand;" onclick="logout()">
        <img src="Image/img03.gif" style="height:20px;width:100px;"/>
        <span style="position:absolute;top:0px;left:23px;font-weight:bold">Log Out</span>
    </div>

    <!--  DIV FOR HOME PAGE AND MANAGE SUBSCRIPTION  -->
    <div class="home" id="home" style="position:absolute;top:35px;left:5px;width:980px;height:100px; background-color:#87CEFA;overflow:auto">
        <span style="position:absolute;top:30px;left:70px;font-weight:bold">Check Feed URL :</span>
        <input id="url"  type="text" size="75" style="position:absolute;top:30px;left:230px;"/>
        <span style="position:absolute;top:70px;left:70px;font-weight:bold">Search Password :</span>
        <input id="searchPass" name="searchPass" type="text" size="75" style="position:absolute;top:70px;left:230px;"/>
        <input type="button" onclick="checkXML('<%=usname%>')" size="14" value="Add URL" style="position:absolute;top:45px;left:750px;"/>
        <p style="position:absolute;top:105px;left:100px;font-weight:bold"></p>

         <!--<a id="home1" href="Manage_Feeds.jsp" target="_self" style="position:absolute;top:60px;left:40px;font-weight:bold;text-decoration:none;cursor:hand;">
            <u> Manage Subscription </u>-->

    </div>
        <div id="Rss_Title" style="position:absolute;top:150px;left:2px;padding:0px 0px 0px 0px;background-color:#FFFFFF;" >
        </div>

        <div style="position:absolute;top:10px;left:50px;cursor:hand;" onclick="Search_His('<%=usname%>')">

            <img src="Image/img03.gif" style="height:20px;width:150px;"/>
            <span style="position:absolute;top:0px;left:23px;font-weight:bold">Search History</span>
        </div>

    <div style="position:absolute;top:10px;left:250px;cursor:hand;" onclick="setting('<%=usname%>')">

            <img src="Image/img03.gif" style="height:20px;width:100px;"/>
            <span style="position:absolute;top:0px;left:28px;font-weight:bold">Setting</span>
        </div>
        </form>

 </body>


Advertisement
View Answers

July 30, 2011 at 1:04 PM


Use dom parser..........

import java.io.*;
import org.w3c.dom.*;
import javax.servlet.*;
import javax.xml.parsers.*;
import javax.servlet.http.*;

public class ReadXML extends HttpServlet{ 
    public boolean isTextNode(Node n){
    return n.getNodeName().equals("#text");
    }
        public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try{
        DocumentBuilderFactory docFactory =  DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse("C:/roseindia.xml");
        out.println("<table border=2><tr><th>Name</th><th>Address</th></tr>");
        Element  element = doc.getDocumentElement(); 
        NodeList personNodes = element.getChildNodes(); 
        for (int i=0; i<personNodes.getLength(); i++){
        Node emp = personNodes.item(i);
        if (isTextNode(emp))
         continue;
        NodeList NameDOBCity = emp.getChildNodes(); 
        out.println("<tr>");
        for (int j=0; j<NameDOBCity.getLength(); j++ ){
        Node node = NameDOBCity.item(j);
        if ( isTextNode(node)) 
        continue;
        out.println("<td>"+(node.getFirstChild().getNodeValue())+"</td>");
        } 
        out.println("</tr>");
        }
        out.println("</table>");
        }
        catch(Exception e){
            System.out.println(e);
        }
        }
}


Related Tutorials/Questions & Answers:
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access Xml which is present in my d drive plz can u should go through my code n tell me the things where i went wrong java servlet program protected void
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
Advertisements
Read XML using Java
of all i need to read xml using java . i did good research in google and came to know...Read XML using Java  Hi All, Good Morning, I have been working.../xmlParsing/java-xml-parsing-using-sax.shtml http://www.roseindia.net/xml/sax
read xml using java
read xml using java  <p>to read multiple attributes and elements from xml in an order.... ex :component name="csl"\layerinterfacefile="poo.c...;   Please visit the following link: Read XML data
xml file display - XML
code to display the above xml file in tree structure where...xml file display   - - - - ADL SCORM CAM 1.3 - - session3 - Online Instructional Strategies that Affect Learner
How to read value from xml using java?
How to read value from xml using java?  Hi All, I want to read value from following xml using java.. In <Line>,data is in format of key and value pair.. i want to read only values..could u plz help me in this?Thanks
how to read this xml file - XML
how to read this xml file  i want to read this xml file using java(using struts2 or using jsp) and i want result as name=admin menu=user... read i have tried lot more , but i am not able to read this xml file
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <...(); } the codes can't read the xml file bcz i want to append the whole xml in my gui. seems the problems is all xml tag must be enum. can you suggest me how to read
xml file reading using java
xml file reading using java  hi deepak I want to read some data from xml file and send that output to particular email address using java   import org.w3c.dom.*; import org.w3c.dom.Node; import javax.xml.parsers.
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have only one element with multiple attributes with same tag name. here is my file... a solution.my java code is given below. import java.io.File; import
To read & write a excel file using the core java
To read & write a excel file using the core java  Hai, I'm new to JavaProgram.But now i need java program to read & write a excel file so, can anyone help me to learn the above mentioned topic(link for the portion
How to read text file in Servlets
How to read text file in Servlets  ... file in servlets. In this example we will use the input stream to read the text.... We will read the content of this file and display in the browser.  Get
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
Retrieve data from xml using servlets
for retrieving the data from xml File using Servlets.   Hi, Do you want to read and parse the XML file in servlet? Please ask your question in detail. Thanks Deepak   Hi, Here is one tutorial: Create XML File using Servlet
simple code to write an read and write the login detail to a xml file using javascript ( username and password )
simple code to write an read and write the login detail to a xml file using... to write and read the login details (username and password )into a xml file using javascript. (XML database
How to Split a large XML file using java?
How to Split a large XML file using java?  How can we split a 500MB Xml file?I know how to split xml file after reading the entire document in a file.Here we cannot load the entire file as it is a large file
JDOM example in java, How to read a xml file in java.
JDOM example in java, How to read a xml file in java. In this tutorial, we will see how to  read a xml file in java. Code. Student.xml...;     File file = new File(data
How read xml file value in dojox chart - XML
How read xml file value in dojox chart  How to read the xml file value in dojox chart
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
display Helloworld through servlets using jboss
display Helloworld through servlets using jboss  I'm beginner,Can You please Write the code for this.Including WEB.INF   Please visit the following link: http://www.roseindia.net/jboss
How to read the data in text file seperated by by ',' in java using IO Operations
How to read the data in text file seperated by by ',' in java using IO Operations  in Text file data like raju 45,56,67 ramu 46,65,78 raji 34,23,56 this is the student marks in text file.this data read and calculate
download xml file from website using java code
download xml file from website using java code  how to download xml file from website using java code
java read file
java read file  Hello i need some help... i want to read an MS Excel file in java so how to read that file
Read file in java
Read file in java  Hi, How to Read file in java? Thanks   Hi, Read complete tutorial with example at Read file in java. Thanks
How to Read Excel file Using Java
How to Read Excel file In this section,you will learn how to read excel file data and display on the console.We have used Jakarta POI api... data is then used to display file values on the console. Here is the code
Java get XML File
Java get XML File       In this section, you will study how the java application read the XML file. For this, you need to create a XML file. Here is the employee.xml
 

 

 

DMCA.com