2017-05-08 112 views
0

XML代碼提取值:PHP的XPath節點

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
    <s:Header> 
     <a:Action s:mustUnderstand="1">http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault</a:Action> 
     <a:RelatesTo>urn:uuid:9C35A55C-A5B4-AC89-AF95-B287E727332E</a:RelatesTo> 
     <ActivityId CorrelationId="427735e4-59b8-4176-a2b7-49f91b965d11" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">00000000-0000-0000-0000-000000000000</ActivityId> 
     <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
      <u:Timestamp u:Id="_0"> 
       <u:Created>2017-05-08T07:34:29.504Z</u:Created> 
       <u:Expires>2017-05-08T07:39:29.504Z</u:Expires> 
      </u:Timestamp> 
     </o:Security> 
    </s:Header> 
    <s:Body> 
     <s:Fault> 
      <s:Code> 
       <s:Value>s:Sender</s:Value> 
       <s:Subcode> 
        <s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</s:Value> 
       </s:Subcode> 
      </s:Code> 
      <s:Reason> 
       <s:Text xml:lang="en-US">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:entity. The InnerException message was 'There was an error deserializing the object of type Microsoft.Xrm.Sdk.Entity. The value 'mehul' cannot be parsed as the type 'Boolean'.'. Please see InnerException for more details.</s:Text> 
      </s:Reason> 
     </s:Fault> 
    </s:Body> 

這裏如何獲得S:使用DOMXPath 我正在嘗試使用下面的代碼錯誤值,但我沒有得到,請幫助我的任何一個

$dom = new \DOMDocument(); 
    $dom->loadXML($crmResponse); 
    $domxpath = new \DOMXPath($dom); 
    $domxpath->registerNamespace('u',"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); 
    $domxpath->registerNamespace('a',"http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher"); 

    //$nodeList = $domxpath->query("//*[local-name()='RetrieveResult']/b:Attributes/b:KeyValuePairOfstringanyType[c:key='new_currentapplicationstatus']/c:value/b:Value/text()"); 
    $nodeList = $domxpath->query("//*[local-name()='RetrieveResult']/o:Security[u:Id='_0']"); 
    $nodeErr = $domxpath->query("//*[local-name()='RetrieveResult']/s:Fault/s:Value/text()"); 

    var_dump($nodeErr);exit; 

這裏如何開始使用DOMXPath查詢PHP屬性值

回答

1

這裏我們使用DOMDocument檢索輸出。

Try this code snippet here

<?php 
ini_set('display_errors', 1); 
$string=' <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
    <s:Header> 
     <a:Action s:mustUnderstand="1">http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault</a:Action> 
     <a:RelatesTo>urn:uuid:9C35A55C-A5B4-AC89-AF95-B287E727332E</a:RelatesTo> 
     <ActivityId CorrelationId="427735e4-59b8-4176-a2b7-49f91b965d11" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">00000000-0000-0000-0000-000000000000</ActivityId> 
     <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
      <u:Timestamp u:Id="_0"> 
       <u:Created>2017-05-08T07:34:29.504Z</u:Created> 
       <u:Expires>2017-05-08T07:39:29.504Z</u:Expires> 
      </u:Timestamp> 
     </o:Security> 
    </s:Header> 
    <s:Body> 
     <s:Fault> 
      <s:Code> 
       <s:Value>s:Sender</s:Value> 
       <s:Subcode> 
        <s:Value xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</s:Value> 
       </s:Subcode> 
      </s:Code> 
      <s:Reason> 
       <s:Text xml:lang="en-US">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:entity. The InnerException message was \'There was an error deserializing the object of type Microsoft.Xrm.Sdk.Entity. The value \'mehul\' cannot be parsed as the type \'Boolean\'.\'. Please see InnerException for more details.</s:Text> 
      </s:Reason> 
     </s:Fault> 
    </s:Body> 
    </s:Envelope>'; 
$domDocument= new DOMDocument(); 
$domDocument->loadXML($string); 
foreach($domDocument->getElementsByTagNameNS("http://www.w3.org/2003/05/soap-envelope", "Value") as $value) 
{ 
    echo $value->textContent; 
    echo PHP_EOL; 
} 

輸出:

s:Sender a:DeserializationFailed

+0

太感謝你了......你的寶貴響應 – somesh

+0

@somesh歡迎...朋友... :) –