2016-11-10 19 views
0

我已經嘗試過,並且一直無法想出一個解決方案。我的問題是這樣的: 我有一個SOAP信封響應是如下...SOAP信封解析與所有XMLNS - 不在Magento上工作

<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soapenv:header> 
     <soapenv:body> 
      <mcu:prescreenusereligibilityresponse xmlns:mcu="http://www.ups.com/XMLSchema/XOLTWS/MCUserEligibility/v1.0"> 
       <common:response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0"> 
        <common:responsestatus> 
         <common:code>1</common:code> 
         <common:description>Success</common:description> 
        </common:responsestatus> 
       </common:response> 
       <mcu:eligibilitystatuscode>3</mcu:eligibilitystatuscode> 
      </mcu:prescreenusereligibilityresponse> 
     </soapenv:body> 
    </soapenv:header> 
</soapenv:envelope> 

我然後訪問我的Mac這樣的元素:

$ns=array(); 
$xml=new SimpleXMLElement($string); 
foreach($xml->getNamespaces(true) as $key=>$url){ 
    $xml->registerXPathNamespace($key, $url); 
    $ns[]=strval($url); 

} 
print_r(strval($xml->children($ns[0])->header->body->children($ns[1])->prescreenusereligibilityresponse->eligibilitystatuscode)); 

用同樣的方法在單獨的Linux實例我在print_r行發生錯誤,說最後一個孩子不能爲空。我已確認這些值是正確的。我也嘗試使用$xml->xpath('//mcu:eligibilitystatuscode')沒有成功。

我真的堅持-_-

回答

1

我用了一個替代方案。 DOMDocument()

我注意到在將不同的對象節點打印到屏幕上後,xml字符串中的大小寫是不同的。我嘗試將名稱空間與正確的大寫字母進行比較,並且仍然沒有使用SimpleXML成功。

$doc = new DOMDocument(); 

      $doc->loadXML($result); 
      if($doc){ 
       foreach($doc->getElementsByTagNameNS('*', '*') as $element){ 
        if($element->tagName=='mcu:EligibilityStatusCode'){ 
         if($element->nodeValue==0){ 
          return true; 
         } 
         else{ 
          return false; 
         } 
        } 
       } 
       return false; 
      } 
      else{ 
       return false; 
      } 

上面是工作代碼,其中result是soap請求返回的xml。我的代碼基本上循環通過xml響應中的每個節點。它依次顯示在Magento前端的一個塊上。

0

考慮,你說這適用於你的Mac,而不是在Linux上,它有可能是行結束的問題。在解析XML之前,嘗試添加它以將所有可能的行結尾替換爲正在運行的系統的默認行。

$string = preg_replace('~\R~u', PHP_EOL, $string); 
+0

同樣的結果。我相當肯定'trim($ string)'會擺脫結束字符。 (我也試過) –

+0

@DrewPeterson修剪只會在整個字符串的開始/結尾處刪除空格,而不是每一行。 –