Get XML values with PHP

1

Colleagues.

I have the following code:

<pais>
    <estado tipo="RJ">
        <bairro nome="listar">
            <local>
                <aparecer>Flamengo</aparecer>
            </local> 
            <local>
                <aparecer>Botafogo</aparecer>
            </local> 
             <local>
                <aparecer>Urca</aparecer>
            </local> 
       </bairro>
    </estado>
</pais>

And catching up with PHP like this:

$arquivo = "bairros.xml";
$xml = simplexml_load_file($arquivo);

foreach ($xml->estado->bairro as $listar) {
    echo $listar->local->aparecer;
    echo "<br>";
}

It only returns me the first neighborhood and not all.

    
asked by anonymous 15.05.2016 / 15:34

1 answer

1

Change your foreach to:

foreach ($xml->estado->bairro->local as $listar) {
    echo $listar->aparecer;
}
    
15.05.2016 / 15:42