如何从此SOAP响应中读取2个节点 - 我可以使用它来处理SimpleXML

时间:2012-07-04 14:39:38

标签: php soap curl 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>

我把头发拉出来了!

1 个答案:

答案 0 :(得分:0)

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

你可以使用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