Quentin Gll Quentin Gll - 1 month ago 11
Java Question

StAX and arraylist java

I'm trying to read an xml document with StAX but I have a little problem and i don't know how to fix it, I've tried to look for over internet (maybe i'm using the wrong key word for my problem :/)
so I've this 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>




here is my parser:

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();
}


and the result is not the one expected:

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


]


and it's the same for the 3 question i have. When i parse "mauvaiseReponse" all the the "mauvaiseReponse" balise are streamed and added.

the result i'm looking for is something like this:

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


i'm sorry if my english isn't that good, i hope you will undestand my problem and can help me with this

Answer

It is happens because all your question share the same instance of list of mauvaisesReponses.

You should create new instance mauvaisesReponses after you add it to your question 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.