我正在尝试用StAX读取xml文档,但我有一点问题,我不知道如何解决它,我试图寻找互联网(也许我使用错误的关键字对于我的问题:/) 所以我有这个XML:
<questionReponses
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://polytis.fr/studentest'
xsi:schemaLocation='http://polytis.fr/studentest qanda.xsd'>
<questionReponse>
<categorie>Biologie</categorie>
<question>Question 1</question>
<reponse>reponse correcte 1</reponse>
<mauvaiseReponse>reponse fausse 1.1</mauvaiseReponse>
<mauvaiseReponse>reponse fausse 1.2</mauvaiseReponse>
<mauvaiseReponse>reponse fausse 1.3</mauvaiseReponse>
</questionReponse>
<questionReponse>
<categorie>Chimie</categorie>
<question>Question 2</question>
<reponse>reponse correcte 2</reponse>
<mauvaiseReponse>reponse fausse 2.1</mauvaiseReponse>
<mauvaiseReponse>reponse fausse 2.2</mauvaiseReponse>
<mauvaiseReponse>reponse fausse 2.3</mauvaiseReponse>
</questionReponse>
<questionReponse>
<categorie>CultureG</categorie>
<question>Question 3</question>
<reponse>reponse correcte 3</reponse>
<mauvaiseReponse>reponse fausse 3.1</mauvaiseReponse>
<mauvaiseReponse>reponse fausse 3.2</mauvaiseReponse>
<mauvaiseReponse>reponse fausse 3.3</mauvaiseReponse>
</questionReponse>
这是我的解析器:
try {
// instanciation du parser
InputStream in = new FileInputStream(nomFichier);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader parser = factory.createXMLStreamReader(in);
// lecture des evenements
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
// traitement selon l'evenement
switch (event) {
case XMLStreamConstants.START_ELEMENT:
break;
case XMLStreamConstants.END_ELEMENT:
if (parser.getLocalName().equals("questionReponse")) {
question = new Question(categorieCourante,questionCourante,bonneReponseCourante,mauvaisesReponses);
quizz.add(question);
}
if (parser.getLocalName().equals("categorie")) {
categorieCourante = donneesCourantes;
}
if (parser.getLocalName().equals("question")) {
questionCourante = donneesCourantes;
}
if (parser.getLocalName().equals("reponse")) {
bonneReponseCourante = donneesCourantes;
}
if (parser.getLocalName().equals("mauvaiseReponse")) {
mauvaisesReponses.add(donneesCourantes);
}
break;
case XMLStreamConstants.CHARACTERS:
donneesCourantes = parser.getText();
break;
} // end switch
} // end for
parser.close();
}
,结果不是预期的结果:
question 1
[categorie =
Biologie
question =
Question 1
bonne reponse =
reponse correcte 1
mauvaises reponse =
reponse fausse 1.1
reponse fausse 1.2
reponse fausse 1.3
reponse fausse 2.1
reponse fausse 2.2
reponse fausse 2.3
reponse fausse 3.1
reponse fausse 3.2
reponse fausse 3.3
, categorie =
Chimie
question =
Question 2
bonne reponse =
reponse correcte 2
mauvaises reponse =
reponse fausse 1.1
reponse fausse 1.2
reponse fausse 1.3
reponse fausse 2.1
reponse fausse 2.2
reponse fausse 2.3
reponse fausse 3.1
reponse fausse 3.2
reponse fausse 3.3
, categorie =
CultureG
question =
Question 3
bonne reponse =
reponse correcte 3
mauvaises reponse =
reponse fausse 1.1
reponse fausse 1.2
reponse fausse 1.3
reponse fausse 2.1
reponse fausse 2.2
reponse fausse 2.3
reponse fausse 3.1
reponse fausse 3.2
reponse fausse 3.3
]
对于我有的3个问题,它是一样的。当我解析“mauvaiseReponse”时,所有的“mauvaiseReponse”应答都会被流式传输并添加。
我正在寻找的结果是这样的:
question 1
categorie =
Biologie
question =
Question 1
bonne reponse =
reponse correcte 1
mauvaises reponse =
reponse fausse 1.1
reponse fausse 1.2
reponse fausse 1.3
我很抱歉,如果我的英语不是那么好,我希望你能解决我的问题并帮助我解决这个问题
答案 0 :(得分:0)
这是因为您的所有问题都共享mauvaisesReponses
列表的相同实例。
在将新实例添加到问题mauvaisesReponses
new Question(categorieCourante,questionCourante,bonneReponseCourante,mauvaisesReponses)
question = new Question(categorieCourante,questionCourante,bonneReponseCourante,mauvaisesReponses);
quizz.add(question);
mauvaisesReponses = new ArrayList<>(); //Or what data structure you use for it.
答案 1 :(得分:0)
简单地说,您必须在每个已完成的问题实例上续订 badAnswers ( mauvaisesReponses )列表。
我为提供的输入xml文件编写了示例代码。为简单起见,我在解决方案的同一文件中创建了问题类;
// A - first instantiation of badAnswers list
List<String> badAnswers = new LinkedList<>();
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
switch (event) {
case XMLStreamConstants.START_ELEMENT:
break;
case XMLStreamConstants.END_ELEMENT:
if (parser.getLocalName().equals("questionReponse")) {
Question question = new Question(currentCategory, currentQuestion, currentRightAnswer, badAnswers);
quiz.add(question);
// B - Renew badAnswers after each Question entry insert
badAnswers = new LinkedList<>();
}
另请注意,我在此处使用LinkedList
实现来证明您的问题与List实现无关,它与实现无关。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
public class Solution {
public static void main(String[] args) {
List<Question> quiz = getQuiz("inputFile.xml");
printQuiz(quiz);
}
public static List<Question> getQuiz(String fileName) {
List<Question> quiz = null;
try {
// parser instantiation
InputStream in = new FileInputStream(fileName);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader parser = factory.createXMLStreamReader(in);
String currentData = null, currentCategory = null, currentQuestion = null, currentRightAnswer = null;
quiz = new LinkedList<>();
List<String> badAnswers = new LinkedList<>(); // first instantiation of badAnswers list
for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
switch (event) {
case XMLStreamConstants.START_ELEMENT:
break;
case XMLStreamConstants.END_ELEMENT:
if (parser.getLocalName().equals("questionReponse")) {
Question question = new Question(currentCategory, currentQuestion, currentRightAnswer, badAnswers);
quiz.add(question);
badAnswers = new LinkedList<>(); // Renew badAnswers after each Question entry insert
}
if (parser.getLocalName().equals("categorie")) {
currentCategory = currentData;
}
if (parser.getLocalName().equals("question")) {
currentQuestion = currentData;
}
if (parser.getLocalName().equals("reponse")) {
currentRightAnswer = currentData;
}
if (parser.getLocalName().equals("mauvaiseReponse")) {
badAnswers.add(currentData);
}
break;
case XMLStreamConstants.CHARACTERS:
currentData = parser.getText();
break;
}
} // end of for loop
parser.close();
} catch (FileNotFoundException | XMLStreamException e) {
e.printStackTrace();
}
return quiz;
}
public static void printQuiz(List<Question> quiz) {
int i = 1;
for(Question q : quiz) {
System.out.println("Question : " + i++);
System.out.printf("\tCategory : %s\n" , q.getCurrentCategory());
System.out.printf("\tQuestion : %s\n" , q.getCurrentQuestion());
System.out.printf("\tAnswer : %s\n" , q.getCurrentRightAnswer());
System.out.printf("\tBad Answers: %s\n" , q.getBadAnswers());
System.out.println("***********************\n");
}
}
}
class Question {
private String currentCategory;
private String currentQuestion;
private String currentRightAnswer;
private List<String> badAnswers;
public Question(String currentCategory, String currentQuestion, String currentRightAnswer, List<String> badAnswers) {
this.currentCategory = currentCategory;
this.currentQuestion = currentQuestion;
this.currentRightAnswer = currentRightAnswer;
this.badAnswers = badAnswers;
}
public String getCurrentCategory() {
return currentCategory;
}
public String getCurrentQuestion() {
return currentQuestion;
}
public String getCurrentRightAnswer() {
return currentRightAnswer;
}
public List<String> getBadAnswers() {
return badAnswers;
}
}
Question : 1
Category : Biologie
Question : Question 1
Answer : reponse correcte 1
Bad Answers: [reponse fausse 1.1, reponse fausse 1.2, reponse fausse 1.3]
***********************
Question : 2
Category : Chimie
Question : Question 2
Answer : reponse correcte 2
Bad Answers: [reponse fausse 2.1, reponse fausse 2.2, reponse fausse 2.3]
***********************
Question : 3
Category : CultureG
Question : Question 3
Answer : reponse correcte 3
Bad Answers: [reponse fausse 3.1, reponse fausse 3.2, reponse fausse 3.3]
***********************