Check XML with php

Asked

Viewed 103 times

3

Colleagues.

I have the following XML.

<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>

How would I get the answer? I’m doing it this way:

$xml = simplexml_load_file($arquivoXML);
 foreach($xml as $notasGabarito){  
  $resXML = $xml->avaliacao->disciplina->questao->resposta;
  echo "Res => " .$resXML."<br>";
 } 
}

Only he’s returning me only the first C.

1 answer

2


You’re getting the index straight, by the way you set up xml you’ll have to go through question to question the answer.

Detail, I don’t know if you put wrong xml but it presents some syntactic errors.

Example:

foo.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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>
   </avaliacao>
</gabarito>

Reading the foo.xml file

$xml = simplexml_load_file('foo.xml');
$questoes = count($xml->avaliacao->disciplina->questao);
for ($i = 0; $i < $questoes; $i++):
   print($xml->avaliacao->disciplina->questao[$i]->resposta);
endfor;

Exit:

SimpleXMLElement Object ( [0] => C ) SimpleXMLElement Object ( [0] => D ) SimpleXMLElement Object ( [0] => A ) 

To read xml attributes:

$disciplina= count($xml->avaliacao->disciplina);
for ($i = 0; $i < $disciplina; $i++):
   print_r($xml->avaliacao->disciplina[$i]->attributes()['nome']);
endfor;
  • Perfect Gabriel, it worked... now what if it’s in the case of feedback? I want to take the name of the disciplines, which are more than one, but I’m also not getting it. As I would in this case?

  • @Jose.Marcos is very simple, makes a feedback Count, if you have 50 jigs, you make a match to the example above just iterating with the variable $i in it, giving a print to debug

  • right... I did as you said, but returns only the name of the first discipline, IE, Mathematics... as I would also to catch Physics?

Browser other questions tagged

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