I am trying to parse XML from a web service result but I am failing. The result seems to be valid as I print it and it displays all data I need.
print_r($result);
Gives me:
<?xml version="1.0" encoding="utf-8"?><retorno> <versao>1.00</versao> <versaodados>1.00</versaodados> <codigoconvenio>S2111601391F5530F0250A</codigoconvenio> <codigoretorno>0</codigoretorno> <mensagens> <mensagem>Sucesso</mensagem> </mensagens> <dados> <dado><identificadorfatura>1000219113262</identificadorfatura><mesanoreferencia>ABR/2015</mesanoreferencia><datavencimento>2015-06-07</datavencimento><valorfatura>239.72</valorfatura></dado> </dados></retorno>
Now, I need to get the "codigoretorno" tag contents and I am trying this considering that "retorno" would be the root tag:
$xml = simplexml_load_string($result);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "<br>", $error->message;
}
} else {
echo 'Codigo Convenio: ';
echo $xml->codigoconvenio;
}
Gives me no errors and just prints "Codigo Convenio: " with nothing following. When I try:
print_r($xml);
I get:
SimpleXMLElement Object ( )
And also:
var_dump($xml);
The output is:
object(SimpleXMLElement)#5 (0) { }
Any suggestions would be welcome!