2014-09-29 68 views
1

這裏是XML(A.XMLSimlpexml和registerxpathnamespace問題與SOAP響應體

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ser="http://www.example.com/v1/services"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ser:getAnalyticalDeliveryEstimatesRequest> 
      <ser:buyer> 
       <ser:buyerId>1233</ser:buyerId> 
       <ser:toCountry>IN</ser:toCountry> 
       <ser:toZip>110001</ser:toZip> 
      </ser:buyer> 
      <ser:item> 
       <ser:id>25164</ser:id> 
       <ser:categoryId>15032</ser:categoryId> 
       <ser:seller> 
        <ser:sellerId>11997</ser:sellerId> 
        <ser:fromCountry>IN</ser:fromCountry> 
       </ser:seller> 
       <ser:transactionId>0</ser:transactionId> 
      </ser:item> 
     </ser:getAnalyticalDeliveryEstimatesRequest> 
    </soapenv:Body> 
</soapenv:Envelope> 

的PHP代碼解析此:

$xml = simplexml_load_file('a.xml', NULL, NULL, 'http://schemas.xmlsoap.org/soap/envelope/'); 
$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/'); 
$xml->registerXPathNamespace('ser', 'http://www.example.com/v1/services'); 

$xpath = $xml->xpath('//soapenv:Body/ser:getAnalyticalDeliveryEstimatesRequest'); 

print_r($xpath); 

它沒有給出數據。

請讓我知道如果我做錯了。

+0

'echo $ xpath [0] - > asXML();'會顯示數據。你的代碼一切都已經很好。 – hakre 2014-10-11 20:09:58

回答

1

這是很奇怪地看到那不是工作,但或者你也可以使用這樣的:

$dom = new DOMDocument(); 
$dom->loadXML($xml); 
$xpath = new DOMXpath($dom); 
$element = $xpath->query('//soapenv:Body/ser:getAnalyticalDeliveryEstimatesRequest'); 

foreach($element->item(0)->childNodes as $node) { 
    // perform your actions here 
} 

Sample Output

編輯:另外這是另一種方式:

$xml_string ='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ser="http://www.example.com/v1/services"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <ser:getAnalyticalDeliveryEstimatesRequest> 
      <ser:buyer> 
       <ser:buyerId>1233</ser:buyerId> 
       <ser:toCountry>IN</ser:toCountry> 
       <ser:toZip>110001</ser:toZip> 
      </ser:buyer> 
      <ser:item> 
       <ser:id>25164</ser:id> 
       <ser:categoryId>15032</ser:categoryId> 
       <ser:seller> 
        <ser:sellerId>11997</ser:sellerId> 
        <ser:fromCountry>IN</ser:fromCountry> 
       </ser:seller> 
       <ser:transactionId>0</ser:transactionId> 
      </ser:item> 
     </ser:getAnalyticalDeliveryEstimatesRequest> 
    </soapenv:Body> 
</soapenv:Envelope>'; 
$xml = simplexml_load_string($xml_string, null, null, 'http://schemas.xmlsoap.org/soap/envelope/'); 
$ns = $xml->getNamespaces(true); 
$soap = $xml->children($ns['soapenv']); 
foreach($soap->Body as $nodes) { 
    $ser = $nodes->children($ns['ser'])->getAnalyticalDeliveryEstimatesRequest; 
    foreach($ser->buyer as $sub_nodes) { // this can also be ->item as well 

    } 
} 
+0

Widd看到simplexml無法做到這一點。謝謝你的幫助。 – 2014-09-29 11:29:08

+0

@ web-nomad是啊我也想知道,無論如何,我很高興這有助於 – Ghost 2014-09-29 11:39:47

+0

hello @ web-nomad我成功了一個替代方案,我並不爲自己的第一個答案感到自豪,如果你想使用'simpleXMLelement',你也可以使用此修訂 – Ghost 2014-09-29 11:51:16

2

首先,在您的示例代碼中,您實際上並沒有在任何地方定義$node

$xpath = $xml->xpath('//soapenv:Body/ser:getAnalyticalDeliveryEstimatesRequest'); 
print_r($node); 

應該是:

$xpath = $xml->xpath('//soapenv:Body/ser:getAnalyticalDeliveryEstimatesRequest'); 
$node = $xpath[0]; 
print_r($node); 

或許:

$xpath = $xml->xpath('//soapenv:Body/ser:getAnalyticalDeliveryEstimatesRequest'); 
foreach ($xpath as $node) { 
    print_r($node); 
} 

其次,print_r是不顯示的SimpleXML對象很大。它給你一個空的輸出並不意味着元素是空的。

例如,嘗試呼應節點發現的名稱(Demo):

$xpath = $xml->xpath('//soapenv:Body/ser:getAnalyticalDeliveryEstimatesRequest'); 
foreach ($xpath as $node) { 
    echo $node->getName(); 
} 

爲了得到它的內容,你需要選擇他們在命名空間中,與the ->children() method,此時即使print_r就能看到他們(雖然也有其他原因不能完全依賴於它)(Demo):

$xpath = $xml->xpath('//soapenv:Body/ser:getAnalyticalDeliveryEstimatesRequest'); 
foreach ($xpath as $node) { 
    print_r($node->children('http://www.example.com/v1/services')); 
} 

嘗試「與命名空間的SimpleXML」更多的例子搜索。