我如何从这个 SOAP 响应中读取 2 个节点 - 我可以用它做 简单XML.


How can I read 2 nodes from this SOAP response - and can I do with it SimpleXML

我熟悉SimpleXML,现在正在接收来自客户端的SOAP响应。我已经尝试通过阅读 SimpleXML 尽可能多地阅读示例,但我就是无法让我的工作!有人可以给我看一行PHP,它用下面的代码读取DESCRIPTION节点:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetPoliciesResponse xmlns="http://www.abraxas-uk.com/Abraxas/InsuranceWebsite/InsWebService/">
      <GetPoliciesResult>
        <Policies xmlns="">
          <Policy>
            <Description>Off-Pad Warranty</Description>
            <Plan />
          </Policy>
        </Policies>
      </GetPoliciesResult>
    </GetPoliciesResponse>
  </soap:Body>
</soap:Envelope>

我要拔头发了!

IIRC将SimpleXML与包含多个XML命名空间的文档一起使用是一团糟。

你可以使用 DOM(即没有 XPath):

<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <GetPoliciesResponse xmlns="http://www.abraxas-uk.com/Abraxas/InsuranceWebsite/InsWebService/">
      <GetPoliciesResult>
        <Policies xmlns="">
          <Policy>
            <Description>Off-Pad Warranty</Description>
            <Plan />
          </Policy>
        </Policies>
      </GetPoliciesResult>
    </GetPoliciesResponse>
  </soap:Body>
</soap:Envelope>';
$doc = DOMDocument::loadXml($xml);
$desc = $doc->getElementsByTagName('Description')->item(0)->textContent;
echo $desc;

对于更复杂的情况,您可能必须使用 XPath 而不是 DOMNode::getElementsByTagName()

http://www.php.net/manual/en/class.domxpath.php

为了您的方便,您可以在两个世界(DOM/Simplexml)之间切换:

http://www.php.net/manual/en/function.simplexml-import-dom.php

http://www.php.net/manual/en/function.dom-import-simplexml.php