如何使用PHP从下面的结果中获取值。
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<p558:registerDonorResponse xmlns:p558="http://ws.ots.labcorp.com">
<p558:registerDonorReturn xmlns:p118="http://data.ws.ots.labcorp.com">
<p118:clientRegistrationId>clr1</p118:clientRegistrationId>
<p118:labcorpRegistrationNumber>100059064</p118:labcorpRegistrationNumber>
<p118:registrationTime>2012-12-01T05:40:51.628Z</p118:registrationTime>
</p558:registerDonorReturn>
</p558:registerDonorResponse>
</soapenv:Body>
</soapenv:Envelope>
感谢。
答案 0 :(得分:1)
您的XML包含以名称空间为前缀的标记,因为它对SOAP响应很常见。
从php的SimpleXML文档中查看以下comment:
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
<p:person id="1">John Doe</p:person>
<p:person id="2">Susie Q. Public</p:person>
</people>
XML;
$sxe = new SimpleXMLElement($xml);
$ns = $sxe->getNamespaces(true);
$child = $sxe->children($ns['p']);
foreach ($child->person as $out_ns)
{
echo $out_ns;
}
在您的情况下,访问属性的代码应该是这样的(它在so.xml文件中针对您的XML进行了测试):
<?php
$xml = file_get_contents('so.xml');
$sxe = simplexml_load_string($xml);
$ns = $sxe->getNamespaces(true);
$child =
$sxe->children($ns['soapenv'])->
Body->children($ns['p558'])->
registerDonorResponse->registerDonorReturn->children($ns['p118']);
var_dump($child);
结果:
$ php -f so.php
object(SimpleXMLElement)#4 (3) {
["clientRegistrationId"]=>
string(4) "clr1"
["labcorpRegistrationNumber"]=>
string(9) "100059064"
["registrationTime"]=>
string(24) "2012-12-01T05:40:51.628Z"
}
但请注意,发出SOAP请求并手动解析响应通常是一种不好的做法,请考虑使用SOAP client。
答案 1 :(得分:0)
你有什么尝试?
无论如何,你可以使用PHP的simplexml_load_file
或全功能DOMDocument class