1

Hi I've this xml from a soap service.I obtain xml with curl. How can access nodes in php? results can have more resulSets

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <ns3:ExampleResponse xmlns:ns3="http://example.com/ptfall" xmlns:ns2="http://example.com/soa">
            <return>
                <ns3:resultSet>
                    <ns3:categoria>
                        <ns2:codice>1</ns2:codice>
                        <ns2:descrizione>Esempio xxx</ns2:descrizione>
                    </ns3:categoria>
                    <ns3:causale>
                        <ns3:codice>_XXXXX</ns3:codice>
                        <ns3:descrizione>Annullo Mancato</ns3:descrizione>
                        <ns3:identificativo>
                            <ns2:long>74</ns2:long>
                        </ns3:identificativo>
                    </ns3:causale>
                    
                </ns3:resultSet>
               
                <ns3:serviceInfo>
                    <ns2:codiceErroreOccorso>0</ns2:codiceErroreOccorso>
                    <ns2:erroreOccorso>false</ns2:erroreOccorso>
                    <ns2:executionId>xxxxxxxxxxx</ns2:executionId>
                    <ns2:tipoErroreOccorso>0</ns2:tipoErroreOccorso>
                </ns3:serviceInfo>
            </return>
        </ns3:ExampleResponse>
    </soap:Body>
</soap:Envelope>

For example i wanted to return just ns3:descrizione node for each resultSet I tried use this but no works

$soap = simplexml_load_string($data);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children()->ExampleResponse->........DUNNO HOW TO DO IT;
echo $response;
Marco Bozzola
  • 179
  • 1
  • 3
  • 17

1 Answers1

1

It's a little more complicated than that, but - for your example of ns3:descrizione - it can be done this way:

$soap = simplexml_load_string($data);
$soap->registerXPathNamespace("ns3", "http://example.com/ptfall");
$resultSet = $soap->xpath('//ns3:resultSet');

foreach ($resultSet as $rs)
{   
    $infos  = $rs->xpath('.//*[local-name()="descrizione"]/text()');
    foreach ($infos as $target) {
        echo $target ."\r\n";
    }   
}

Output:

Esempio xxx
Annullo Mancato
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45