Jump to content

XML to JSP


theredking
 Share

Recommended Posts

I am trying to get questions for a multiple choice quiz stored in an xml file displayed on a JSP. I'm new to XML and am stuck as how to get the questions onto the JSP. I know that JDOM and mapping to a database are options but i am not finding examples on the web very helpful. Can anybody suggesat the easiest way to do this. Thank You. I have attached the xml for clarity.

<?xml version="1.0" ?><?xml-stylesheet type="text/xsl" href="quizdisplaypd.xsl"?><!--  <!DOCTYPE XMultipleChoice SYSTEM "quizpd.dtd">   -->  <XMultipleChoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="quizschemapd.xsd"> 	<QuizName>Parmjeet Dayal Great XML Assignment Quiz</QuizName>	<Units>  <Unit> 	 <QuestionNo>1</QuestionNo> 	 <Subject>Dave Chappelle Show</Subject> 	 <Question>Who is Rick James?</Question> 	 <Answer Corr="Y">Rick James</Answer> 	 <Answer>Charlie Murphy</Answer> 	 <Answer>Dave Chappelle</Answer> 	 <Answer>Eddie Murphy</Answer>  </Unit>  <Unit> 	 <QuestionNo>2</QuestionNo> 	 <Subject>Dave Chappelle Show</Subject> 	 <Question>Should you put feet on other peoples couches?</Question> 	 <Answer>Yes</Answer> 	 <Answer>No</Answer> 	 <Answer>In someone else's house</Answer> 	 <Answer Corr="Y">In Eddie Murphy's house, he can afford it</Answer>  </Unit>  <Unit> 	 <QuestionNo>3</QuestionNo> 	 <Subject>Wrestling</Subject> 	 <Question>Who is the greatest wrestler of all time?</Question> 	 <Answer>Hulk Hogan</Answer> 	 <Answer>The Rock</Answer> 	 <Answer Corr="Y">Goldberg</Answer> 	 <Answer>Stone Cold Steve Austin</Answer>  </Unit>	  <Unit> 	 <QuestionNo>4</QuestionNo> 	 <Subject>Music</Subject> 	 <Question>Who is the lead singer of The Stone Roses?</Question> 	 <Answer>Noel Gallagher</Answer> 	 <Answer Corr="Y">Ian Brown</Answer> 	 <Answer>Chris Martin</Answer> 	 <Answer>Snoop Dog</Answer>  </Unit>  <Unit> 	 <QuestionNo>5</QuestionNo> 	 <Subject>The A Team</Subject> 	 <Question>Which of these men takes pity on fools?</Question> 	 <Answer>Murdock</Answer> 	 <Answer>Face</Answer> 	 <Answer Corr="Y">Mr T</Answer> 	 <Answer>Hannibal</Answer>  </Unit>	  <Unit> 	 <QuestionNo>6</QuestionNo> 	 <Subject>Star Wars</Subject> 	 <Question>Master Yoda is the last great practitioner of Ataru. Who is the greatest Soresu practitioner?</Question> 	 <Answer Corr="Y">Mace Windu</Answer> 	 <Answer>Darth Vader</Answer> 	 <Answer>Chewwy</Answer> 	 <Answer>Hans Solo</Answer>  </Unit>  <Unit> 	 <QuestionNo>7</QuestionNo> 	 <Subject>Seseame Steet</Subject> 	 <Question>Which of these growchy individuals lives in a trash can?</Question> 	 <Answer>Big Bird</Answer> 	 <Answer Corr="Y">Oscar</Answer> 	 <Answer>The Count</Answer> 	 <Answer>Elmo</Answer>  </Unit>  <Unit> 	 <QuestionNo>8</QuestionNo> 	 <Subject>Lord of the Rings</Subject> 	 <Question>Mithrandir is Sindarin name of which powerful character?</Question> 	 <Answer>Saruman</Answer> 	 <Answer>Pippin</Answer> 	 <Answer>Balrog</Answer> 	 <Answer Corr="Y">Gandalf</Answer>  </Unit>  <Unit> 	 <QuestionNo>9</QuestionNo> 	 <Subject>Cricket</Subject> 	 <Question>How is the most dangerous man in world cricket today?</Question> 	 <Answer>Matthew Hoggard</Answer> 	 <Answer Corr="Y">Mahendra Singh Dhoni</Answer> 	 <Answer>Ricky Ponting</Answer> 	 <Answer>Shaun Bond</Answer>  </Unit>  <Unit> 	 <QuestionNo>10</QuestionNo> 	 <Subject>Animal Fights</Subject> 	 <Question>In a fight who would win, a Lion or a Grizzly Bear?</Question> 	 <Answer>Lion</Answer> 	 <Answer>Bear</Answer> 	 <Answer>Draw</Answer> 	 <Answer Corr="Y">They'd discuss their issues and end it in a civilised manner</Answer>  </Unit>		</Units></XMultipleChoice>

Link to comment
Share on other sites

try this in an XSL file:

<?xml version="1.0" ?><xml-stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><html><xsl:for-each select="Unit"><xsl:value-of select="QuestionNo" /><b><xsl-value-of select="Subject" /></b>:<xsl:value-of select="Question" /><xsl:for-each select="Answer[@correct!='Y']"><input type="radio"><xsl:attribute name="name">   <xsl:value-of select="'Answer'" />   <xsl:value-of select="QuestionNo" /></xsl:attribute><xsl:attribute name="value">   <xsl:value-of select="Answer" /></xsl:attribute></input><br /></xsl:for-each><br /></xsl:for-each></html></xsl:stylesheet>

If you save this in a file on the server with a ".xsl" extension then you can link to it using the following link at the top of your xml file:

<? xml-stylesheet type="text/xsl" href="newxslfilename.xsl" ?>

This is only a presentation layout, but you can change the code so that you have a <script> section under the html tag to evaluate the questions. The XSL tutorials on w3schools.com will help you a lot with this, and let me know if there is a problem with the @correct bit of this code.Hope this helps :)Dooberry

Link to comment
Share on other sites

Actually, from what I understand, he would like to execute the XSLT transformation which I gave him in this topic with JSP, simmilarly to the ASP example on the site. Correct? Or maybe check the answers with JSP?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...