Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_jsp thread: need help on jsp and xml urgently


Message #1 by "nadim idun-ogde" <midan2@h...> on Thu, 17 Jan 2002 17:02:12
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">

<question>
<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"/>
<question>

<question>

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

<question>
</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;
}


i also want to know how i can write a class to match each correct answer
(in the xml file9 with the one the user chooses...thanx

  Return to Index