2017-05-08 108 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欢迎...朋友... :) –