我熟悉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>
我把头发拉出来了!
答案 0 :(得分:0)
你可以使用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