Help with XML in PhP

-2

Well, I have the following XML:

<erros>
    <erro>
       <description>Nao localizado</description>
    </erro>
    <erro>
       <description>Nao especificado</description>
    </erro>
</erros>

And I want to know how I get the 2 errors together in php since if I put: $xml->erros->erro->description; will open only one error

    
asked by anonymous 05.03.2016 / 19:11

1 answer

1

Well this is simple to imagine that each block of data is a "node", that is, each time you repeat your block you will have a position that corresponds to an array, so just run all positions that will have all the data .

I do not know if this is loading the xml of a file or a string but a very simple conceptual example would be:

<?php
// Seus dados em XML
$xml = '<erros>
 <erro>
  <description>Nao localizado</description>
 </erro>
 <erro>
  <description>Nao especificado</description>
 </erro>
</erros>';

// Carrega os "nos" do XML usando a função do PHP 
$xml = simplexml_load_string($xml);

// Cria uma variável onde ira juntar os resultados
$errors = "";

// Faz o loop com a leitura de todos blocos de erros
foreach ($xml->erro as $er) {
 // Concatena os errros
 $errors .= $er->description . "\r\n";
}

// Apresenta os erros na tela
echo $errors;
?> 
    
05.03.2016 / 21:28