使用PHP中的命名空间解析SOAP响应

use*_*852 3 php soap simplexml xml-namespaces xml-parsing

任何建议如何解析这个SOAP响应和获得的价值namereport_type?请注意,有两个名称实例; 一个在report_type另一个之下severity.

这是SOAP响应

<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>
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的PHP代码:

<?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>';               
    } 
?>
Run Code Online (Sandbox Code Playgroud)

PHP代码不会回显任何内容.当我使用($xml->xpath('//ns1:name') as $item)它时,返回两个名称(原始漏洞和HIGH).

我知道我错过了一些愚蠢的东西.你能帮帮忙吗?先感谢您!

Scu*_*zzy 5

首先,我已经纠正了这个元素

</ns:return>
Run Code Online (Sandbox Code Playgroud)

并改为

</ns1:return>
Run Code Online (Sandbox Code Playgroud)

我似乎通过在两个xpath段中复制名称空间前缀来获得您所追求的结果

$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>';
}            
Run Code Online (Sandbox Code Playgroud)

产量

Name: Original Vulnerability
Run Code Online (Sandbox Code Playgroud)