aspose file tools*
The moose likes JSP and the fly likes need help on jsp and xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "need help on jsp and xml" Watch "need help on jsp and xml" New topic

need help on jsp and xml

midan myaa

Joined: Nov 26, 2001
Posts: 8
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.*;

public class DOMBean implements {
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 questionNumber="Question 2">
<questionText>The_____method is invoked once when an applet begins execution</questionText>
<choice>None of the above</choice>
<marks ifCorrect="1.0"/>
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="*" %>
<%@ 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();

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

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

<input type="Submit" value="Submit">
and this is a question type class...
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.util.*;
import Dombean.*;
public class MCQuestionType
DOMBean b = new DOMBean();
private Document doc;
private Element row;
String str1;
public MCQuestionType()
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 agree. Here's the link:
subject: need help on jsp and xml
Similar Threads
help on jsp and xml
DOM (xml) question
adding more attributes+DOM
Error while compiling
need urgent help on JSP and XMl PLEASE!!!!!!