• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help on jsp and xml

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i am a new user of JSP and i am trying to write a a web questionnaire. I have stored the questions as xml and want to read them and print them out on a browser but i'm a stuck. i acyually have been able to print out one question.... ie a method to print out the question and another to print out the choices in a javabean but i want to have one method to that will hold both the question and the choices so i can iterate it to print all the questions or something like that..can anyone help me do this.......i'll drop some code
This class gets the xml file, Elements and values of the xml file
package Dombean;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;

public class DOMBean implements java.io.Serializable {
public DOMBean() {
}
public static Document getDocument(String file) throws Exception {
// Step 1: create a DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// Step 2: create a DocumentBuilder
DocumentBuilder db = dbf.newDocumentBuilder();
// Step 3: parse the input file to get
// a Document object
Document doc = db.parse(new File(file));
return doc;
}
public static String getValue(Element e, String name, int index)
{
NodeList nl = e.getElementsByTagName(name);
Element e2 = (Element)nl.item(index);
String qt = ((Text)e2.getFirstChild()).getData();
return qt;
}
public static Element getElement( Document doc , String tagName ,
int index ){
//given an XML document and a tag
//return an Element at a given index
NodeList list = doc.getDocumentElement().getElementsByTagName(
tagName );
return (Element)list.item( index );
}
This is the xml file
<?xml version="1.0" encoding="us-ascii"?>
<tutorial title="Java Fundamentals" code="06_02132" tutor="Nadim">
<mc_QuestionType questionNumber="Question 1">
<questionText>A Method is invoked with a.......</questionText>
<choice>Method Call</choice>
<choice>Local Variable</choice>
<choice>Return Statement</choice>
<choice>None of the above</choice>
<answer>Method Call</answer>
<marks ifCorrect="1.0"/>
</mc_QuestionType>
<mc_QuestionType questionNumber="Question 2">
<questionText>The_____method is invoked once when an applet begins execution</questionText>
<choice>Main</choice>
<choice>Init</choice>
<choice>Start</choice>
<choice>None of the above</choice>
<answer>Init</answer>
<marks ifCorrect="1.0"/>
</mc_QuestionType>
</tutorial>
This is the jsp file that prints a the questions
<%@ page contentType="text/html"%>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="Dombean.*" %>
<%! String tutorialName; %>
<jsp:useBean id="domparser" class="Dombean.DOMBean" />
<jsp:useBean id="b" class="MCQuestionType" />
<%
Document doc = domparser.getDocument("C:\\Tomcat\\FYProject\\lib\\Questions.xml");
tutorialName = doc.getDocumentElement().getAttribute("title").trim();

%>
<html>
<body bgcolor="#ffffcc">
<form action="check.jsp" method=POST>
<center>
<h2><%=tutorialName %></h2>
</center>

<%=str88 %><br>
<%int optioncounter = 0;
int question=0;
ArrayList answers = b.printChoices();
Iterator qi = answers.iterator();
while(qi.hasNext())
{
optioncounter++;
%>
<input type="checkbox" name="answer" value="option<%= optioncounter %>"><%=(String)qi.next() %><br>
<% } %>

<br><br>
<input type="Submit" value="Submit">
</form>
</body>
</html>
and this is a question type class...
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import Dombean.*;
public class MCQuestionType
{
DOMBean b = new DOMBean();
private Document doc;
private Element row;
String str1;
public MCQuestionType()
{
try{
doc = b.getDocument("C:\\Tomcat\\FYProject\\lib\\Questions.xml");
}catch(Exception e){}
ArrayList li = new ArrayList();
row = b.getElement(doc,"mc_QuestionType",1);
}
public String printQuestion()
{
String s = b.getValue(row,"questionText",0);
return s;
}
public ArrayList printChoices()
{
ArrayList list= new ArrayList();
for(int i=0;i<4;i++){
list. add(b.getValue(row,"choice",i));
}
return list;
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic