0

我熟悉 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>

我正在拔头发!

4

1 回答 1

0

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

于 2012-07-04T15:05:27.953 回答