2012-11-19 137 views
3

任何有關如何解析此SOAP響應並獲得report_type的值name的建議?注意有兩個名字的例子;一個在report_type下,另一個在severity下。使用PHP中的名稱空間解析SOAP響應

這裏是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> 

這裏是我使用的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>';    
    } 
?> 

的PHP代碼沒有任何迴音。當我使用($xml->xpath('//ns1:name') as $item)時,它返回兩個名稱(原始漏洞和高)。

我知道我錯過了一些愚蠢的東西。你能幫忙嗎?先謝謝你!

+1

請問爲什麼需要首先解析XML?您是否未使用內置的SOAP客戶端並通過其他方式獲取此數據? – Scuzzy

回答

5

首先,我已經糾正了這個元素

</ns:return> 

,並把它改成

</ns1:return> 

我似乎得到的結果您可以通過在這兩個細分的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>'; 
}    

輸出

Name: Original Vulnerability 
+0

工作!謝謝謝謝! – user1794852