So Amigo, in this first line we already have an error, you are calling the variable $xml that does not yet exist.
<?php
$simples = new SimpleXMLElement($xml);
$xml = simplexml_load_file('http://vallesw.azurewebsites.net/api/municipiosfranqueados');
About creating the simpleXMLElement object of the xml document, when you use the simplexml_load_file()
it reads the XML document and returns an object of type Simplexmlelement (Note: if the XML was badly formed it returns false), then it does not need to instantiate a Simplexmlelement, as it was done in the first line, just do:
<?php
$xml = simplexml_load_file('http://vallesw.azurewebsites.net/api/municipiosfranqueados');
$segundo = $xml->NewDataSet->CarregaMunicipioFranqueados[1];
$codigo = $segundo->Codigo_Municipio;
$municipio = $segundo->Municipio;
echo "Esse é o codigo: $codigo";
echo "<br/>";
echo "Esse é o municipio: $municipio";
?>
Only that there is a detail, I did some tests here and noticed another problem, is with a problem in the content-type of the header of the page that displays the xml, and when the simplexml_load_file($url)
loaded xml, gave an error: Start tag expected, '<'
, then I made the following correction:
<?php
$url = "http://vallesw.azurewebsites.net/api/municipiosfranqueados";
$xml = file_get_contents($url); // Pega o conteúdo da url e armazena em string
$xml = stripcslashes($xml); // remove '\n','\r' da string
$xml = str_replace("\"", "",$xml); // remover aspas dulas do inicio e fim da string.
$xml = simplexml_load_string($xml); // ler a string e retorna um objeto SimpleXMLElement
$segundo = $xml->CarregaMunicipioFranqueados[1];
$codigo = $segundo->Codigo_Municipio;
$municipio= $segundo->Municipio;
echo "Esse é o codigo: $codigo";
echo "<br/>";
echo "Esse é o municipio: $municipio";
?>
I hope I was able to help man.
Victor Thank you is exactly what I was trying to do and I was having a hard time. Thank you for your help!
– Robsonrrn