我是这个网站的新手,所以如果您对我耐心,我将不胜感激。 :) 我正在完善我正在制作的一种学习工具程序。 用户可以在设置中输入问题/答案信息(就像在Quizlet中一样)。要保存程序,您必须单击“保存”按钮。
但是由于某种原因,仅在单击保存按钮之后,XML文件(存储问题/答案的地方)才会更新,然后停止运行该程序,然后再次运行该程序。
[简而言之,直到程序关闭然后重新打开后,更新的信息才会显示在文本框中。]
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg1051037_chang_chinesejeopardygame;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import jdk.internal.org.xml.sax.SAXException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* @author 장리나
*/
public class ParseXMlFile {
private DocumentBuilderFactory builderFactory;
private DocumentBuilder builder;
private Document document;
private File inputFile;
public static List<QuestionDetails> questionDetailsList;
// parses the xml file
public ParseXMlFile() {
// the try statement gets the different question details
try {
inputFile = new File("QuestionDetails.xml");
builderFactory = DocumentBuilderFactory.newInstance();
builder = builderFactory.newDocumentBuilder();
document = builder.parse(inputFile);
} catch (ParserConfigurationException ex) {
// the catch statement stops the program when there is no value
Logger.getLogger(ParseXMlFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (org.xml.sax.SAXException ex) {
// catch statement stops the program when there is no value
Logger.getLogger(ParseXMlFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
// catch statement stops the program when there is no value
Logger.getLogger(ParseXMlFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void addNode(String questionId, String question, String multipleChoice1, String multipleChoice2, String multipleChoice3, String rightAnswerChoice)
{
//
}
// updates the question details to xml file (edits preexisting info)
public boolean updateQuestionDetailsToXML(String questionId, String question, String multipleChoice1, String multipleChoice2, String multipleChoice3, String rightAnswerChoice) {
boolean success = false;
try {
// Get the questionDetails Tag
NodeList nodeList = document.getElementsByTagName(JeopardyConstants.QUESTION_DETAILS_TAG);
for(int i = 0; i < nodeList.getLength(); i++) {
Element questionElement = (Element) nodeList.item(i);
NamedNodeMap nodeMapAttr = questionElement.getAttributes();
Node attrNode = nodeMapAttr.getNamedItem(JeopardyConstants.ID_TAG);
if (attrNode.getNodeValue().equalsIgnoreCase(questionId)) {
// update the question
NodeList questionNodeList = questionElement.getChildNodes();
for (int j = 0; j < questionNodeList.getLength(); j++) {
Node childNode = questionNodeList.item(j);
if (childNode.getNodeName().equalsIgnoreCase(JeopardyConstants.QUESTION_TAG)) {
childNode.setTextContent(question);
}
}
String rightChoice1 = "N";
String rightChoice2 = "N";
String rightChoice3 = "N";
// update the multipleChoices
if (rightAnswerChoice.equalsIgnoreCase(JeopardyConstants.CHOICE1_TAG)) {
rightChoice1 = "Y";
} else if (rightAnswerChoice.equalsIgnoreCase(JeopardyConstants.CHOICE2_TAG)) {
rightChoice2 = "Y";
} else if (rightAnswerChoice.equalsIgnoreCase(JeopardyConstants.CHOICE3_TAG)) {
rightChoice3 = "Y";
}
// Update multiple choice 1
updateMultipleChoiceDetails(questionElement, questionId, JeopardyConstants.CHOICE1_TAG, multipleChoice1, rightChoice1);
updateMultipleChoiceDetails(questionElement, questionId, JeopardyConstants.CHOICE2_TAG, multipleChoice2, rightChoice2);
updateMultipleChoiceDetails(questionElement, questionId, JeopardyConstants.CHOICE3_TAG, multipleChoice3, rightChoice3);
}
}
updateXML(); // update the xml file
// loop through the question details
for (QuestionDetails questionDetails : questionDetailsList) {
if (questionDetails.getId().equalsIgnoreCase(questionId)) {
questionDetails.setQuestion(question);
}
}
success = true;
} catch (TransformerException ex) {
// if success is not equal to true
Logger.getLogger(ParseXMlFile.class.getName()).log(Level.SEVERE, null, ex);
}
return success;
}
// fix this part below? (empty method)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void addNewEntry(String questionId, String newQuestion, String choice1Answer, String choice2Answer,String choice3Answer) {
}
private void updateMultipleChoiceDetails(Element questionElement, String questionId, String choiceName, String updatedValue, String rightAnswer) {
NodeList choiceNodeList = questionElement.getElementsByTagName(choiceName);
for (int k = 0; k < choiceNodeList.getLength(); k++) {
Element choiceElement = (Element) choiceNodeList.item(k);
// get the child nodes
NodeList choiceChildrenList = choiceElement.getChildNodes();
for (int m = 0; m < choiceChildrenList.getLength(); m++) {
if (choiceChildrenList.item(m).getNodeType() == Node.ELEMENT_NODE) {
Element choiceChild = (Element) choiceChildrenList.item(m);
String nodeName = choiceChild.getTagName();
// Update the value/choice details
if (nodeName.equalsIgnoreCase(JeopardyConstants.VALUE_TAG)) {
choiceChild.getFirstChild().setTextContent(updatedValue);
for (QuestionDetails questionDetails : questionDetailsList) {
if (questionDetails.getId().equalsIgnoreCase(questionId)) {
// Update to QuestionDetails Object
if (choiceName.equalsIgnoreCase(JeopardyConstants.CHOICE1_TAG)) {
questionDetails.setMultipleChoice1(updatedValue);
} else if (choiceName.equalsIgnoreCase(JeopardyConstants.CHOICE2_TAG)) {
questionDetails.setMultipleChoice2(updatedValue);
} else if (choiceName.equalsIgnoreCase(JeopardyConstants.CHOICE3_TAG)) {
questionDetails.setMultipleChoice3(updatedValue);
}
}
}
}
// WORK IN PROGRESS -- DOES NOT WORK YET
if (nodeName.equalsIgnoreCase(JeopardyConstants.CORRECT_TAG)) {
// Update the 'correct' tag if the answer choice is the correct answer
choiceChild.getFirstChild().setTextContent(rightAnswer);
for (QuestionDetails questionDetails : questionDetailsList) {
if (questionDetails.getId().equalsIgnoreCase(questionId)) {
questionDetails.setRightAnswer(updatedValue);
}
}
}
}
}
}
}
private void updateXML() throws TransformerConfigurationException, TransformerException {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult result = new StreamResult(inputFile);
transformer.transform(domSource, result);
}
public void parseXML() {
questionDetailsList = new ArrayList<QuestionDetails>();
NodeList nodeList = document.getElementsByTagName(JeopardyConstants.QUESTION_DETAILS_TAG);
for (int i = 0; i < nodeList.getLength(); i++) {
Element questionElement = (Element) nodeList.item(i);
String attribute = questionElement.getAttribute(JeopardyConstants.ID_TAG);
QuestionDetails questionDetails = new QuestionDetails();
questionDetails.setId(attribute);
NodeList levelNodeList = questionElement.getElementsByTagName(JeopardyConstants.LEVEL_TAG);
for (int k = 0; k < levelNodeList.getLength(); k++) {
Element element = (Element) levelNodeList.item(k);
String nodeName = element.getFirstChild().getNodeName();
String nodeValue = element.getFirstChild().getTextContent();
//System.out.println(nodeName + ":" + nodeValue);
questionDetails.setLevel(Integer.parseInt(nodeValue));
}
NodeList questionNodeList = questionElement.getElementsByTagName(JeopardyConstants.QUESTION_TAG);
for (int k = 0; k < questionNodeList.getLength(); k++) {
Element element = (Element) questionNodeList.item(k);
String nodeName = element.getFirstChild().getNodeName();
String nodeValue = element.getFirstChild().getTextContent();
//System.out.println(nodeName + ":" + nodeValue);
questionDetails.setQuestion(nodeValue);
}
NodeList scoreNodeList = questionElement.getElementsByTagName(JeopardyConstants.SCORE_TAG);
for (int k = 0; k < scoreNodeList.getLength(); k++) {
Element element = (Element) scoreNodeList.item(k);
String nodeName = element.getFirstChild().getNodeName();
String nodeValue = element.getFirstChild().getTextContent();
//System.out.println(nodeName + ":" + nodeValue);
questionDetails.setScore(Integer.parseInt(nodeValue));
}
NodeList choice1NodeList = questionElement.getElementsByTagName(JeopardyConstants.CHOICE1_TAG);
for (int k = 0; k < choice1NodeList.getLength(); k++) {
Element choice1Element = (Element) choice1NodeList.item(k);
NodeList choice1ChildrenList = choice1Element.getChildNodes();
for (int m = 0; m < choice1ChildrenList.getLength(); m++) {
if (choice1ChildrenList.item(m).getNodeType() == Node.ELEMENT_NODE) {
Element choice1Child = (Element) choice1ChildrenList.item(m);
String nodeName = choice1Child.getTagName();
String nodeValue = choice1Child.getFirstChild().getTextContent();
// System.out.println(">>>>>>:" + nodeName);
if (nodeName.equalsIgnoreCase(JeopardyConstants.VALUE_TAG)) {
questionDetails.setMultipleChoice1(nodeValue);
}
if (nodeName.equalsIgnoreCase(JeopardyConstants.CORRECT_TAG) && nodeValue.trim().equalsIgnoreCase("Y")) {
questionDetails.setRightAnswer(questionDetails.getMultipleChoice1());
}
}
}
}
// Get nodelist of tag "choice2"
NodeList choice2NodeList = questionElement.getElementsByTagName(JeopardyConstants.CHOICE2_TAG);
//Loop through the nodelIst
for (int k = 0; k < choice2NodeList.getLength(); k++) {
//Get the first element in the node and get its child nodes
Element choice2Element = (Element) choice2NodeList.item(k);
NodeList choice2ChildrenList = choice2Element.getChildNodes();
//Loop through the child nodes
for (int m = 0; m < choice2ChildrenList.getLength(); m++) {
if (choice2ChildrenList.item(m).getNodeType() == Node.ELEMENT_NODE) {
Element choice2Child = (Element) choice2ChildrenList.item(m);
//get the tag name and the tag value
String nodeName = choice2Child.getTagName();
String nodeValue = choice2Child.getFirstChild().getTextContent();
// System.out.println(">>>>>>:" + nodeName);
if (nodeName.equalsIgnoreCase(JeopardyConstants.VALUE_TAG)) {
questionDetails.setMultipleChoice2(nodeValue);
}
if (nodeName.equalsIgnoreCase(JeopardyConstants.CORRECT_TAG) && nodeValue.trim().equalsIgnoreCase("Y")) {
questionDetails.setRightAnswer(questionDetails.getMultipleChoice2());
}
}
}
}
NodeList choice3NodeList = questionElement.getElementsByTagName(JeopardyConstants.CHOICE3_TAG);
for (int k = 0; k < choice3NodeList.getLength(); k++) {
Element choice3Element = (Element) choice3NodeList.item(k);
NodeList choice3ChildrenList = choice3Element.getChildNodes();
for (int m = 0; m < choice3ChildrenList.getLength(); m++) {
if (choice3ChildrenList.item(m).getNodeType() == Node.ELEMENT_NODE) {
Element choice3Child = (Element) choice3ChildrenList.item(m);
String nodeName = choice3Child.getTagName();
String nodeValue = choice3Child.getFirstChild().getTextContent();
// System.out.println(">>>>>>:" + nodeName);
if (nodeName.equalsIgnoreCase(JeopardyConstants.VALUE_TAG)) {
questionDetails.setMultipleChoice3(nodeValue);
}
if (nodeName.equalsIgnoreCase(JeopardyConstants.CORRECT_TAG) && nodeValue.trim().equalsIgnoreCase("Y")) {
questionDetails.setRightAnswer(questionDetails.getMultipleChoice3());
}
}
}
}
questionDetailsList.add(questionDetails);
}
}
/* public static void main(String args[]){
new ParseXMlFile().parseXML();
for(int i=0; i<questionDetailsList.size(); i++){
QuestionDetails detail= questionDetailsList.get(i);
System.out.println("Question Details--------"+detail.toString());
}
}*/
}