1

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!

CJ_COIMBRA
  • 319
  • 1
  • 3
  • 13

1 Answers1

1

The PHP code you have actually is not lying to you and does what you've been written down. Probably it's useful to you to comment on some details:

$xml = simplexml_load_string($result);
if ($xml === false) {
    ...

As you've observed, the false case is not triggered, so loading the XML was successful.

Then you report on the empty looking print_r SimpleXMLElement output. Now this is a bit unfair. You might have learned from observation that print_r and var_dump can be useful in developing and debugging your code. This is especially not true in case of SimpleXMLElement. In general this is not true, because a step-debugger with breakpoints is far superior than placing var_dump or print_r here and there temporarily in your code.

Anyway, for SimpleXMLElement it's specifically not useful. Compare the XML file you oben with a little database. When you create a PDO object to interact with the database for example and you use print_r or var_dump on it, would you expect to see the whole content of the database? I bet you won't. This is similar with simplexml. Using print_r won't show you the XML. It sometimes does show something that resembles the XML structure but only the elements under certain rules which can be pretty specific. By the way, this has always been a point of much befuddlement, a classic in this Q&A is:

Instead, take a look on the XML itself and then query the data you're looking for. As you did. It even does work: https://eval.in/392059.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thanks for the answer. But how come it does not work in my case? Could be some sort of configuration issue in the php at my server? – CJ_COIMBRA Jul 03 '15 at 21:53
  • Well, *good* question ^^. Is the XML code and example code in this question *verbatim* with your problem? – hakre Jul 03 '15 at 21:58
  • The only difference I noted is this << – CJ_COIMBRA Jul 03 '15 at 22:04
  • Oh one more thing that I noted after posting the question is that when I output the $result contents to a text file it gives me the < as < and > as > – CJ_COIMBRA Jul 03 '15 at 22:09
  • That is only a way on how to write (define) a string value: see https://php.net/string - look for *heredoc* and compare to your string usage. So which PHP version are you using and which operating system? – hakre Jul 03 '15 at 22:10
  • for "`<`" and "`>`" this is HTML encoding of "`<`" and "`>`", two characters commonly used in XML/HTML so define elements (tags) which are normally interpreted by a HTML browser (your webbrowser) and are perhaps then HTMl encoded for some display reason. I can't see the encoding done in your example. However if the XML file contains that data already encoded, it then won't be displayed by your browser (or might, sorry for being unprecise). – hakre Jul 03 '15 at 22:12
  • It ended up being another thing. Completely noobness from my part, I was overwriting $result with $client->responseData shortly before the chunk of code I posted. All I needed was to keep $result original and everything worked fine. – CJ_COIMBRA Jul 04 '15 at 16:38
  • So your code was different and that the reason :) - Can happen. Here is a good rule to not let it happen for Stackoverflow: Don't post copy&paste from original code, just create a new example from scratch that isolates the issue you'd like to ask about with as little data/code as necessary to demonstrate the question. This is also a good debugging tool, because it often already helps to learn more quickly while creating the demo-case. – hakre Jul 05 '15 at 12:21
  • Thanks for the valuable advices. – CJ_COIMBRA Jul 05 '15 at 15:42