2014-02-07 76 views
0

我有一个soap xml响应,我想要获取leadReferenceNumber节点的值。如何解析SOAP xml以获取某个节点的值

我不知道我是否正确地做了。

这里是我的代码:

$xml = '<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns2:CreateLeadResponse xmlns:ns9="http://www.momentum.co.za/crm/service/type/fault/SystemFault/v1.0" xmlns:ns8="http://www.momentum.co.za/crm/service/type/ProductCategoryType/v1.0" xmlns:ns7="http://www.momentum.co.za/crm/service/type/CampaignType/v1.0" xmlns:ns6="http://www.momentum.co.za/crm/service/type/PreferredContactMethodType/v1.0" xmlns:ns5="http://www.momentum.co.za/crm/service/type/LanguageType/v1.0" xmlns:ns4="http://www.momentum.co.za/crm/service/type/TitleType/v1.0" xmlns:ns3="http://www.momentum.co.za/crm/service/type/application/Lead/v1.0" xmlns:ns2="http://www.momentum.co.za/crm/service/application/CRMLeadService/v1.0"><leadReferenceNumber>LP1391743548576</leadReferenceNumber></ns2:CreateLeadResponse></soapenv:Body></soapenv:Envelope>'; 

$soap = simplexml_load_string($xml); 
$response = $soap->children(); 
$test = (string)$response->leadReferenceNumber; 

echo $test; 

回答

0

我使用xpath找到了一个解决方案。代码如下:

$xml = '<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ns2:CreateLeadResponse xmlns:ns9="http://www.momentum.co.za/crm/service/type/fault/SystemFault/v1.0" xmlns:ns8="http://www.momentum.co.za/crm/service/type/ProductCategoryType/v1.0" xmlns:ns7="http://www.momentum.co.za/crm/service/type/CampaignType/v1.0" xmlns:ns6="http://www.momentum.co.za/crm/service/type/PreferredContactMethodType/v1.0" xmlns:ns5="http://www.momentum.co.za/crm/service/type/LanguageType/v1.0" xmlns:ns4="http://www.momentum.co.za/crm/service/type/TitleType/v1.0" xmlns:ns3="http://www.momentum.co.za/crm/service/type/application/Lead/v1.0" xmlns:ns2="http://www.momentum.co.za/crm/service/application/CRMLeadService/v1.0"><leadReferenceNumber>LP1391743548576</leadReferenceNumber></ns2:CreateLeadResponse></soapenv:Body></soapenv:Envelope>'; 

$xml = simplexml_load_string($xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/"); 
$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/'); 

foreach($xml->xpath('//soapenv:Body') as $header) 
{ 
    $arr = $header->xpath('//leadReferenceNumber'); // Should output 'something'. 
    $leadid = $arr[0]; 
    echo $leadid; 
}