questions from an xml file

questions from an xml file

i am developing online bit by bit exam for that i retrieved questions from an xml file but when i retrieved using jsp i am getting all questions at a time in a single page.but i want to show one question in one page for the next question user need to click on next buttun and also there is a timer for each question after time has completed system automatically moves to next question with out clicking on next buttun?for that i tried different coding but i didn't get what i actually want?can some one help me in this?urgent?

View Answers

April 21, 2012 at 12:44 PM

1)Servlet.java

import java.io.*;
import java.util.*;
import form.Student;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet extends HttpServlet {
    int offset;
    int length;
    List list;

    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        int maxEntriesPerPage = 1;
        int page = 1;
        String pageNumberValue = request.getParameter("pageNumber");

        if (pageNumberValue != null) {
            try {
                page = Integer.parseInt(pageNumberValue);
                System.out.println("Page Number:" + page);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        int offset = maxEntriesPerPage * (page - 1);
        TestList(offset, maxEntriesPerPage);

        HttpSession httpSession = request.getSession();
        httpSession.setAttribute("pages", getPages());
        httpSession.setAttribute("studentDetails", getListByLength());

        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/paging.jsp");
        dispatcher.forward(request, response);
    }
    public void fillList() {
        list = new ArrayList();
       try{
  File file = new File("c:\\file.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document document = db.parse(file);
  document.getDocumentElement().normalize();
  System.out.println("Root element "+ document.getDocumentElement().getNodeName());
  NodeList node = document.getElementsByTagName("student");
  System.out.println("Information of the students");

  for(int i = 0; i < node.getLength(); i++){
  Node firstNode = node.item(i);
  if(firstNode.getNodeType()== Node.ELEMENT_NODE){
  Element element = (Element) firstNode;
  NodeList firstNameElemntList = element.getElementsByTagName("name");
  Element firstNameElement = (Element) firstNameElemntList.item(0);
  NodeList firstName = firstNameElement.getChildNodes();
  Node n1=firstName.item(0);
  String name=n1.getNodeValue();

  NodeList lastNameElementList = element.getElementsByTagName("address");
  Element lastNameElement = (Element)lastNameElementList.item(0);
  NodeList lastName = lastNameElement.getChildNodes();
  Node n2=lastName.item(0);
  String address=n2.getNodeValue();
  list.add(new Student(name,address));
   }
  }
  for(Student st:list){
  System.out.println(st.getName()+"\t"+st.getAddress());
  }
  }
  catch (Exception e) {
  e.printStackTrace();
  }
    }
    public void TestList(int offset, int length) {
        this.offset = offset;
        this.length = length;
        fillList();
    }
    public ArrayList getListByLength() {
        ArrayList arrayList = new ArrayList();
        int to = this.offset + this.length;
        if (this.offset > list.size())
            this.offset = list.size();
        if (to > list.size())
            to = list.size();
        for (int i = this.offset; i < to; i++) {
            arrayList.add(list.get(i));
        }
        return arrayList;
    }
    public List getPages() {
        List pageNumbers = new ArrayList();
        int pages = list.size() / this.length;
        if (list.size() % this.length != 0) {
            pages = pages + 1;
        }
        for (int i = 1; i <= pages; i++) {
            pageNumbers.add(new Integer(i));
        }
        return pageNumbers;
    }

April 21, 2012 at 12:46 PM

continue..

2)Student.java:

package form;
import java.io.*;

public class Student implements Serializable {

    private String name;
    private String address;


    public Student(String name, String address) {
        this.name = name;
        this.address = address;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

The above java class is a bean and should be located inside /classes/form/Student.

3)paging.jsp:

<%@page import="java.util.List"%>
<%@page import="form.Student"%>
<html>
<h1>Pagination</h1>
<%
    List list = (List) session.getAttribute("studentDetails");
    List pageNumbers = (List) session.getAttribute("pages");
%>
<table border="1">
    <tr><th>Name</th><th>Address</th><th>Age</th><th>Degree</th></tr>
    <%
        for (int i = 0; i < list.size(); i++) {
        Student st = (Student) list.get(i);
        %>
                <tr>
                <td><%=st.getName()%></td>
                <td><%=st.getAddress()%></td>

        <%
        }
        %>
        <td border=1 align="right">
        <form method="get" action="../PaginationServlet">
        <table>
            <tr>
                <%
                    for (int i = 0; i < pageNumbers.size(); i++) {
                %>
                <td><a href="/examples/PaginationServlet?pageNumber=<%=pageNumbers.get(i)%>"><%=pageNumbers.get(i)%></a></td>
                <%
                    }
                %>
            </tr>
        </table>
        </form>
        </td>
    </tr>
</table>
</html>









Related Tutorials/Questions & Answers:
questions from an xml file
questions from an xml file  i am developing online bit by bit exam for that i retrieved questions from an xml file but when i retrieved using jsp i am getting all questions at a time in a single page.but i want to show one
How to create one xml file from existing xml file's body?
How to create one xml file from existing xml file's body?  Hi, i'm working with content optimization system.I want to know how we can take all data from an xml doc's body to develope another xml with that content.I'm using JDOm
Advertisements
graph generation from xml design file
graph generation from xml design file  how to search words in XML file with javascript. i have copied contents of XML file into word file. using searched keywords i would like to draw graph
data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou
Loading properties from a XML file
Loading properties from a XML file       This Example shows you how to Load properties from a XML... parsers. Some of the methods used for loading properties from a XML file
Reading XML from a File
Reading XML from a File       This Example shows you how to Load Properties from the XML file via... to create new DOM parsers. Some of the methods used for reading XML from
Get Data From the XML File
Get Data From the XML File       Here you will learn to retrieve data from XML file using SAX parser... of characters to read from an array.  ADS_TO_REPLACE_2 Here is the XML File
Passing values in ComboBox from XML file
Passing values in ComboBox from XML file In this tutorial we are going... from an XML document. For this what we need  a XML file in which we have... the data from the XML file and insert it into the ComboBox. To make a program over
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
Insert element into an existing xml file - Java Interview Questions
Insert element into an existing xml file  Dear all, how can i insert elements into an existing xml file? now when i am going to insert new elements... the example code at http://www.roseindia.net/xml/dom/xml-tutorial.shtml Post your
JAXB Create XML File And Get Data From XML
JAXB Create XML File And Get Data From XML In this section we will read about how to create XML file and how to convert XML file's data to Java Object... to write the Java Objects into a XML file and unmarshal() method to read XML
Sava data from Form to XML file using strutrs
Sava data from Form to XML file using strutrs  I'am a biginner with struts want so save data from my form in an Xml file using struts but i'm searching witout finding a solution thanks fo your help
Insert element into an existing xml file - Java Interview Questions
Insert element into an existing xml file  thanks for the reply , I... = "firstName"; final String lName = "lastName"; FileWriter file = new FileWriter("contact.xml",true); PrintWriter print = new PrintWriter(file
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
Fetch Records from SQL database and convert into XML file
Fetch Records from SQL database and convert into XML file  Hi Experts... and output XML file with all records into a single XML with containing as Row's. Please see the output XML: - I need output XML File as: - **ADS_TO_REPLACE_2
Data needs to be gathered in XML file from the database (MySql) using JSP
Data needs to be gathered in XML file from the database (MySql) using JSP ... data regarding particular id from the database table. Data needs to be gathered in XML file from the database (MySql) using appropriate JSP/Java Bean functions
Create XML file from flat file and data insert into database
Create XML file from flat file and data insert into database... have developed an application to create xml file from flat file and data... After run Successfully Create an  XML file "flatfile.xml"
Getting Data from XML File (Document)
Getting Data from XML File (Document)   ... from a XML file. All xml files store the data. You can add and modify the data..._TO_REPLACE_1 This program helps you in retrieving the data from a XML file
Problem to print from my properties file - Java Server Faces Questions
Problem to print from my properties file  Hi, I am a new user of this site. It is very interesting. So this is my problem: I have a jsp file where i try to print some messages from my properties file but when i execute
JDOM Element Example, How to add and remove child from xml file.
JDOM Element Example, How to add and remove child from xml file. In this tutorial, we will see how to add and remove child node in an xml Document tree. We can add child any where in xml document tree with the help of JDOM API
Problem to print from my properties file - Java Server Faces Questions
Problem to print from my properties file  Hi, I am a new user of this site. It is very interesting. So this is my problem: I have a jsp file where i try to print some messages from my properties file but when i execute
Storing Data (Retrieved from a XML Document) to a File
Storing Data (Retrieved from a XML Document) to a File... to store data (retrieved from the XML document) to a specified file (with ... format. After running this program asks you a xml file name at the console
Creating XMl file - XML
Creating XMl file   I went on this page: http://www.roseindia.net/xml/dom/createblankdomdocument.shtml and it shows me how to create an XML file, however there is something I don't understand. I have to create an XML file
Retrieving Data From the XML file
Retrieving Data From the XML file  ....  By this example we are going to get the XML data from the xml file in our... is to retrieve the data from the xml file. This xml file has all the information about
xml file display - XML
xml file display   - - - - ADL SCORM CAM 1.3 - - session3 - Online Instructional Strategies that Affect Learner... code to display the above xml file in tree structure where
Accessing XML file from Java
Accessing XML file from Java   ... example with the source code that will make it possible to access the XML file through Java. For that we have used DOM parser. DOM parser that loads the XML file
Sorting Xml file - XML
Sorting Xml file  I have an xml file like this: Smith USA... sort my xml file. Here is my xslt..., my ouput file will be like this: Amy AUC Bob USA John UK
splitting large xml file. - XML
splitting large xml file.  Hi, I have a large xml file(1G) and a schema (XSD) i need to split this xml to small xml files (~20M) that will be valid by the schema
xml file creation in java
xml file creation in java  how to create xml file in java so that input should not be given from keyboard. and that file should be stored.   Please visit the following links: http://www.roseindia.net/tutorial/java/xml
xml - Servlet Interview Questions
Java servlet xml request  How to create a java servlet xml request
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
JDOM Element Example, How to remove child of an element from xml file in java.
JDOM Element Example, How to remove child of an element from xml file in java. In this tutorial, we will see how to remove child of an element from xml file... for manipulating, creating and serializing  xml file. With the help of this java
How to create XML file - XML
language has many API's to create xml file from program.Here is the code example...()); }}Thanks  Hi,Java programming language has many API's to create xml file from...How to create XML file  Creating a XML file, need an example. Thanks
executio of xml file
executio of xml file  how to execute xml file , on web browser
Getting next Tag in the XML File
Getting next Tag in the XML File       This Example shows you how to get the next Tag from the XML File. JAXP (Java API for XML Processing) is an interface which provides parsing
xml configuration file - JDBC
xml configuration file  Hi, Could you please tell me how to write a xml configuration file . We have mysql database in some other system. I have... to create a xml file. Please help me out. Thank you  Hi Friend, Try
Run XML file
Run XML file  Hi.. How do I execute or run an XML file? please tell me about thatADS_TO_REPLACE_1 Thanks
retrieve in xml file in struts2
can i get back the value from the xml file in the ajax function and print...retrieve in xml file in struts2  i am using struts2 and trying... properties file using ajax. i now put the values from properties file to map
how to read this xml file - XML
how to read this xml file  i want to read this xml file using java... read i have tried lot more , but i am not able to read this xml file...[])throws Exception { File f=new File("C:/data.xml"); DocumentBuilderFactory
How to prepare XML file?
How to prepare XML file?  Hi, I want to prepare XML File, can you... you create an XML file.. Create XML File using Servlet How to generate build.xml file Complete Hibernate 3.0 and Hibernate 4 Tutorial Thanks
create a xml from sql server 2005 - XML
create a xml from sql server 2005  hello Dear, i want to know how we create a xml file which retrieve data from Sql server 2005 using java. i... with the name as 'student' i want to create a xml file from the table values which
how to convert .xml file to csv file
how to convert .xml file to csv file  how to convert .xml file to csv file
HttpServlet reading XML from HttpRequest - XML
HttpServlet reading XML from HttpRequest   Hi , My requirement... 2.HttpServlet has to read XML from HttpRequest and put it in a string. HttpServlet should only read the XML from HttpRequest and put it to a string. Can any
how to update xml from java - XML
how to update xml from java  hi, Im new to xml parsing and dont know much about. I need to modify the attribute val of a tag in a complex xml file.... Suppose we have one xml file named "document.xml" document.xml
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
Reading a xml file - JSP-Servlet
Reading a xml file  how to read a xml file using jsp and then i have to retrive a data from that file use it in code?  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/parsing-xml.shtml
how to seach a keyword in a xml file - XML
how to seach a keyword in a xml file  i have a xml file. i wanted to read and store all the tag values. i have a html form in which when i enter some keyword say "a" then it has to display the tag values starting with a letter
retrieving xml document from database
retrieving xml document from database  Hi Guys, I want to retrieve the xml document stored in the database on to a jsp page using jdbc/odbc connnection.Please help me in implementing this feature. Thank You Madhu
parsing XML file to get java object - XML
parsing XML file to get java object  Hello, I'm facing a problem in parsing XML file to get the java object. I've tried to retrieve data from XML file using SAX parser. my XML file structure is the following
Store XML file into Sqlserver Database
Store XML file into Sqlserver Database  Code for storing a typical XML file into SQl server database in 1 or more tables using asp.net using C

Ads