Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old January 17th, 2002, 02:02 PM
midan midan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: England
Posts: 0 midan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
need help on jsp and xml urgently

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 also want to know how i can write a class to check each correct answer(in the xml file9with the one the user chooses...thanx.

.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;
}

Reply With Quote
  #2  
Old January 20th, 2002, 06:35 AM
Michael_Bray Michael_Bray is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Sydney, NSW, Australia
Posts: 40 Michael_Bray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to Michael_Bray
I've never used XML before, just read about it so this may be wrong, but it appears to me that you aren't closing the <question> tags... You open it, then try to close it with a <question> instead of a </question>

If you are printing one, and not all of them that would seem to be the error to me...
__________________
Cheers,
Michael Bray

Reply With Quote
  #3  
Old January 20th, 2002, 11:09 AM
midan midan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2002
Location: England
Posts: 0 midan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thank michael but thats just a typo...its actually </question>..but thats not the problem i am having....my main problem here is the design and using jsp with javabeans...you see i'm building a web-based questionnaire where anyone can pick a subject from a list and answer some questions that have been stored as xml files...so what i'm trying to do is write jsp's and Beans to extract the questions with its multiple choices and print them on a browser, so that when i click a send button or something it fowards it to another bean or jsp to check the answers with the right answer and print the overall mark........


i woukld appreciate any help you can give me as i am

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > need help on jsp and xml urgently


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway