【问题标题】:PHP Xpath extracting a value for a nodePHP Xpath为节点提取值
【发布时间】:2017-05-08 09:58:36
【问题描述】:

XML 代码:

 <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>

这里如何使用 DOMXPath 获取 s:Fault 值 我正在尝试使用下面的代码,但我没有得到请帮助我任何人

   $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属性值

【问题讨论】:

    标签: php xml xpath domdocument


    【解决方案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

    【讨论】:

    • 非常感谢...您的宝贵回复
    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2012-09-17
    • 2016-02-24
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多