Get XML values with PHP

Asked

Viewed 556 times

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 taking with PHP this way:

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

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

He just returns me the first neighborhood and not all.

1 answer

2


Change your foreach to:

foreach ($xml->estado->bairro->local as $listar) {
    echo $listar->aparecer;
}
  • Bingo William. Thank you!

Browser other questions tagged

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