compare xml with user responses

Asked

Viewed 120 times

1

colleagues. I have this code:

<gabarito>
   <avaliacao tipo="Prova" codigo="01" segmento="Ensino Médio" serie="Pré-Vestibular" questoes="20">
      <disciplina nome="Matemática">
         <questao numero="1" alternativas="A,B,C,D,E">
            <resposta>C</resposta>
         </questao>
         <questao numero="2" alternativas="A,B,C,D,E">
            <resposta>D</resposta>
         </questao>
         <questao numero="3" alternativas="A,B,C,D,E">
            <resposta>A</resposta>
         </questao>
      </disciplina>

      <disciplina nome="Física">
         <questao numero="1" alternativas="A,B,C,D,E">
            <resposta>C</resposta>
         </questao>
         <questao numero="2" alternativas="A,B,C,D,E">
            <resposta>D</resposta>
         </questao>
         <questao numero="3" alternativas="A,B,C,D,E">
            <resposta>A</resposta>
         </questao>
      </disciplina>
   </avaliacao>
</gabarito>

Only that the students answer the templates by the system and is stored in this way in the bank:

C,D,A,C,A,E,A,B,C,C,B,D,A,C,D,D,B,B,B,A

I would like to compare the answers of the students with that of the feedback, count and separate by each discipline. Ex.:

Mathematics => 2 correct answers; Physics => 4 certain;

It is possible to do this?

My initial code is this:

foreach($xml->avaliacao->disciplina as $disciplina) {
   $res = $disciplina->attributes();
   if($res["nome"]){
       $questoes = count($xml->avaliacao->disciplina->questao);
       echo $questoes;
   }                                         
}

But I’m not making any progress...

1 answer

1


I don’t really understand where the correct answers and answers are so you can determine the amount of hits, but I believe this will help you understand how to search the data you want in the file.

What you need to do is go through the disciplines and issues within each discipline, see the example:

$xml = simplexml_load_file('gabarito.xml');
//percorre as disciplinas
foreach($xml->avaliacao->disciplina as $disciplina) {
    // imprime o nome da disciplina
    echo $disciplina->attributes()->nome.'<br>'; 
    //percorre as questões
    foreach ($disciplina as $questao) { 
        echo $questao->attributes()->numero . ':' . $questao->resposta.'<br>';
    }


}

This code generates the output below according to the data of your file:

Matemática
1:C
2:D
3:A
Física
1:C
2:D
3:A
  • 1

    Abfurlan... it worked... I couldn’t have done it without your help...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.