#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    England
    Posts
    0
    Rep 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;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Sydney, NSW, Australia
    Posts
    40
    Rep Power
    14
    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
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    England
    Posts
    0
    Rep 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

IMN logo majestic logo threadwatch logo seochat tools logo