0

I found lot of solutions for this problem, but my code won't work!

XML INFO:

<?xml version="1.0" encoding="UTF-8"?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:resultado xmlns:ns2="http://webservice.consulta.spcjava.spcbrasil.org/" data="2014-06-03T11:37:32.001-03:00" restricao="false">
             <protocolo digito="2" numero="1204248496" />
             .... other XML info

MY CODE:

$s = '<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:resultado xmlns:ns2="http://webservice.consulta.spcjava.spcbrasil.org/" data="2014-06-03T11:37:32.001-03:00" restricao="false"><protocolo digito="2" numero="1204248496" /> ...

$xml = simplexml_load_string($s);

$x2 = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://webservice.consulta.spcjava.spcbrasil.org/')->resultado->protocolo->digito;

print_r($x2);
var_dump(count($x2));

Returns null and 0 for the object count.

I've been following this tutorial: http://amigotechnotes.wordpress.com/2013/11/16/parse-xml-with-namespace-by-simplexml-in-php/

I don't get where my example differs from his example. :/

Can anyone help me with this issue, please?

hakre
  • 193,403
  • 52
  • 435
  • 836
Felipe Carminati
  • 297
  • 1
  • 5
  • 16

1 Answers1

0

Finally made it work using another syntax.

$xml = simplexml_load_string($resultadoDocumento[0]["cdo_xml"]);
$resultadoSPC = $xml->children('S', TRUE)->Body->children('ns2', TRUE)->children();

accessing nodes by:

<?php foreach ($resultadoSPC->consumidor->children()->{"consumidor-pessoa-fisica"} as $consumidorElement) : ?>
<?php echo $consumidorElement->attributes()->{"nome"}; ?>
Felipe Carminati
  • 297
  • 1
  • 5
  • 16
  • 1
    If you got it to work with the prefixes, create a second variant with the URIs because prefixes can change, URIs not. For your example they should work interchangeably, so the code in your answer *is technically not different* from your question. So the question already contained the answer which means that your answer (in this sense) is not a real answer. You should better make clear to which concrete analysis you came to what the root-cause of your problem was. – hakre Jun 12 '14 at 19:00
  • Thanks for the tips @hakre. I'll accept the answer because using this syntax made it work, which helped me when I needed it working. Other questions and examples differed from my XML structure, and as I'm a newbie using XML files, using similar structure would help me solve my problem ASAP. I hope it helps someone else. – Felipe Carminati Jun 16 '14 at 13:28