I am trying to create a Java application that would generate Multiple Choice Question. The multiple choice questions are stored in the XML file and validated with DTD or schema. The proposed idea is to create Java app with model view controller design pattern and accessing the XML file. But I am still in an initial phase and basically encountered problem here. I have managed to access almost all element of XML file but could access particular element. I hope you guys could help me out.
Well my XML File is as following,
<?xml version="1.0"?>
<!DOCTYPE question-group SYSTEM "../dtds/something.dtd">
<question-group>
<question-label>something</question-label>
<title>something</title>
<mcqs>
<question>something</question>
<answers>
<option>
<label>something</label>
<statement>something</statement>
</option>
<option>
<label>something</label>
<statement>something</statement>
</option>
<option>
<label>something</label>
<statement>something</statement>
</option>
<option>
<label>something</label>
<statement>something</statement>
</option>
</answers>
<actual-answers>
<answer-label>something</answer-label>
<answer-label>something</answer-label>
</actual-answers>
</mcqs>
</question-group>
My question is "How to access the element is <answer-label tag>?" My Java file is as following:
public class ReadAndDisplay {
public static void main(String args[]) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("xml/something.xml"));
//To normalise text representation
doc.getDocumentElement().normalize();
//---------- To read mcqs
NodeList multipleQuestion;
multipleQuestion = doc.getElementsByTagName("mcqs");
int totalQuestions = multipleQuestion.getLength();
System.out.println("Total number of questions : " + totalQuestions);
for(int n=0; n<totalQuestions; n++) {
Node Question = multipleQuestion.item(n);
if((Question.getNodeType()==Node.ELEMENT_NODE)) {
Element questionElement = (Element)Question;
//-------
NodeList questionList = questionElement.getElementsByTagName("question");
Element qElement = (Element)questionList.item(0);
NodeList textQuestionList = qElement.getChildNodes();
System.out.println("Question : " + ((Node)textQuestionList.item(0)).getNodeValue().tr im());
//-------
NodeList multipleAnswerList = doc.getElementsByTagName("option");
//int totalAnswerList = multipleAnswerList.getLength();
//System.out.println("Total probable answers: " + totalAnswerList);
for(int t=0; t<totalAnswerList.getLength();t++) {
Node answerNode = multipleAnswerList.item(t);
if(answerNode.getNodeType()==Node.ELEMENT_NODE) {
Element answerElement = (Element)answerNode;
//-------
NodeList labelList = answerElement.getElementsByTagName("label");
Element labelElement = (Element)labelList.item(0);
NodeList textLabelList = labelElement.getChildNodes();
System.out.println("Label : " + ((Node)textLabelList.item(0)).getNodeValue().trim( ));
//-------
NodeList statementList = answerElement.getElementsByTagName("statement");
Element statementElement = (Element)statementList.item(0);
NodeList textStatementList = statementElement.getChildNodes();
System.out.println("Statement : " + ((Node)textStatementList.item(0)).getNodeValue().t rim());
} //end of if clause
} //end of for loop with t var
//--
NodeList actualAnswerList = doc.getElementsByTagName("actual-answers");
int totalActualAnswerList = actualAnswerList.getLength();
System.out.println("Total answers: " + totalActualAnswerList);
for(int a=0; a<actualAnswerList.getLength();a++) {
Node actualAnswerNode = actualAnswerList.item(a);
if(actualAnswerNode.getNodeType()==Node.ELEMENT_NO DE) {
Element actualAnswerElement = (Element)actualAnswerNode;
//------
NodeList answerLabelList = actualAnswerElement.getElementsByTagName("answer-label");
Element answerLabelElement = (Element)answerLabelList.item(0);
NodeList textAnswerLabelList = answerLabelElement.getChildNodes();
System.out.println("Actual Answer Label : " + ((Node)textAnswerLabelList.item(0)).getNodeValue() .trim());
} //end of if clause
} //end of for loop with a var
} //end of if clause
} //end of for loop with n var
}
Currently, there are lots of flaws in the coding as it prints many number of times than it intends to. Could you please suggest? Am I in the right path to create Java application with relation to XML? Your suggestions and tips will be mostly welcomed. Cheers