3

Any recommendations how to parse this SOAP response and obtain the value of name for the report_type? Notice there are two instances of name; one under report_type and the other under severity.

Here is the SOAP response

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns1:getIRResponse xmlns:ns1="http://ws.icontent.idefense.com/V3/2">
         <ns1:return xsi:type="ns1:IRResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <ns1:report_type>
               <ns1:id>0</ns1:id>
               <ns1:name>Original Vulnerability</ns1:name>
            </ns1:report_type>
            <ns1:severity>
                <ns1:id>0</ns1:id>
                <ns1:name>HIGH</ns1:name>
            </ns1:severity>
         </ns:return>
      </ns1:getIRResponse>
   </soapenv:Body>
</soapenv:Envelope>

Here is the PHP code I'm using:

<?php        
    $xml = simplexml_load_string($response);    
    $xml->registerXPathNamespace('ns1', 'http://ws.icontent.idefense.com/V3/2');

    foreach ($xml->xpath('//ns1:report_type/name') as $item)
    {
        echo 'Name: '.$item,'<br>';               
    } 
?>

The PHP code doesn't echo anything. When I use ($xml->xpath('//ns1:name') as $item) it returns both names (Original Vulnerability and HIGH).

I know I'm missing something stupid. Can you help please? Thank you in advance!

hakre
  • 193,403
  • 52
  • 435
  • 836
user1794852
  • 83
  • 1
  • 5
  • 1
    Can I ask why the need to parse XML in the first place? Are you not using the built in SOAP client and sourcing this data via some other means? – Scuzzy Nov 19 '12 at 21:48

1 Answers1

6

Firstly, I've corrected this element

</ns:return>

and changed it to

</ns1:return>

I seem to get the result you're after by duplicating the namespace prefix in both xpath segments

$xml = simplexml_load_string($response);    
$xml->registerXPathNamespace('ns1','http://ws.icontent.idefense.com/V3/2');
foreach ($xml->xpath('//ns1:report_type/ns1:name') as $item)
{
  echo 'Name: '.$item,'<br>';
}            

output

Name: Original Vulnerability
Scuzzy
  • 12,186
  • 1
  • 46
  • 46