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?

0

1 Answer 1

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"}; ?> 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.