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 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>

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
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
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
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.
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 file using InputStreamReader in java
how to read file using InputStreamReader in java  Hi, I want to learn to use the InputStreamReader class of Java and trying to read a text file with the class. how to read file using InputStreamReader in java? Thanks  
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
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...-INF/ directory. We will read the content of this file and display in the browser
Retrieve data from xml using servlets
Retrieve data from xml using servlets  Hi plz send me the code 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.ADS
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
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
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
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(data);       DOMBuilder builder 
read and write a file using javascript
read and write a file using javascript  How to read and write a file using javascript
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
Program to read the text from a file and display it on a JFrame.
Program to read the text from a file and display it on a JFrame.  import javax.swing.*; import java.io.*; import java.lang.*; import java.awt.*; class MegaViewer1 extends JFrame { JTabbedPane jtp1=new JTabbedPane
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
how to read file line by line using filereader in java
how to read file line by line using filereader in java  Hi, how to read file line by line using filereader in java? Thanks   Hi, This is example code: package my.code; import java.io.BufferedReader; import
Read Complex XML using DOM/SAX Parser.
Read Complex XML using DOM/SAX Parser.  I have a XML file which is having two types of Items. I want to extract all details of a type only. My XML goes something like this <Movie> <Bollywood> <Actor> Shah Rukh
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
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
validation in struts2 using .xml file
validation in struts2 using .xml file  how to do xml validation in struts2 on dynamic fields   Hello Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/struts/struts2/struts2validation
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
Java Read .doc file using POI library
Java Read .doc file using POI library In this section, you will learn how to read the word document file using POI library. The class HWPFDocument throw all of the Word file data and the class WordExtractor extract the text from
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
How to read and display data from a .properties file from a jsp page
How to read and display data from a .properties file from a jsp page  I have a .properties file. I have to create a jsp page such that it reads the data from this .properties file and display it in table format. Ex:by using
How to Read a file line by line using BufferedReader?
How to Read a file line by line using BufferedReader?  Hello Java... problem is to find the best way to read the file in Java. I just searched the google... to Read a file line by line using BufferedReader, efficiently and using less memory
read from file and store using hash map
read from file and store using hash map  I was stuck with a java project where I have to read a file with two different concepts and store them differently in hashmap. My data file would be something like Adults: Name, xyz
Reading xml file using dom parser in java with out using getelementby tag name
Reading xml file using dom parser in java with out using getelementby tag name  Hi, How to read the xml file using java with dom parser, but without using getelementbytag name, and also read the attribute values also. I had
Writing a file using servlets - JSP-Servlet
Writing a file using servlets  I'm using a servlet to read an input from a jsp file and write into a .txt file in my computer. The code is good until reading the data and creating a file in my space. but it is not writing
form based file upload using servlets
form based file upload using servlets  Hai all, I wrote a program to upload a file into specified directory and store the file path and username... file path it uploaded successfully but in database table it gives null value
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
Java read binary file
Java read binary file  I want Java read binary file example code... at Reading binary file into byte array in Java. Thanks   Hi, There is many more examples at Java File - Learn how to handle files in Java with Examples
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
Java read file
There are many ways to read a file in Java. DataInputStream class is used to read text File line by line. BufferedReader is also used to read a file in Java...(in)); readline() is used to read the next line. Example of Java Read File:ADS_TO_REPLACE_3
XML parsing using Java - XML
XML parsing using Java  I'm trying to parse a big XML file in JAVA..." in element "MIRate"(THE XML code is below).once he enters coverage rate I need to display initial and renewal rate. Like if he enter all the attribute values starting
Read Specific Line from file Using Java
Read Specific Line from file Using Java Here we are going to read a specific line from the text file. For this we have created a for loop to read lines 1... of BufferedReader class read that particular line and display
Display data from xml file to Swings - Swing AWT
Display data from xml file to Swings  Hi, We are Preparing a stand alone application. Where the Swings is the front end. There will be only... clicking the buttons in swings it has to display 20 record in one shot in table

Ads