Help with XML in Php

Asked

Viewed 73 times

-2

Guys, 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 mistake

1 answer

1


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

I don’t know if this 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;
?> 

Browser other questions tagged

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