[英]SimpleXML SOAP response Namespace issues in PHP
I'm having a seemingly impossible time getting my head around simple XML and soap. 我似乎不太可能了解简单的XML和soap。 I've read in a file to simple xml, and this is the result to show it's read in properly:
我已将文件读入简单的xml,这是显示它已正确读入的结果:
echo $garages->asXML();
// result
<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<GetListOfFittingCentresResponse xmlns="http://TyreMen.com/UkTyreNetwork/">
<GetListOfFittingCentresResult xmlns:a="http://TyreMen.com/UkTyreNetwork/Response/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Method>GetListOfFittingCentres</a:Method>
<a:Result>
<a:Code>0</a:Code>
<a:Description>Call completed successfully</a:Description>
</a:Result>
<a:FittingCentres xmlns:b="http://TyreMen.com/UkTyreNetwork/DataTypes/">
<b:FittingCentre>
<b:Address1>Tyremen</b:Address1>
<b:Address2>Witty Street</b:Address2>
<b:Address3>Hull</b:Address3>
<b:Address4/>
<b:BranchName>Witty Street</b:BranchName>
<b:GEOLocation>
<b:Latitude>53.732342619574524</b:Latitude>
<b:Longitude>-0.3738183264892996</b:Longitude>
</b:GEOLocation>
</b:FittingCentre>
</a:FittingCentres>
</GetListOfFittingCentresResult>
</GetListOfFittingCentresResponse>
</s:Body>
</s:Envelope>
But I can't for the life of me work out how to reference any of that data. 但是我无法终生解决如何引用这些数据。 I've tried both these:
我已经尝试过这两个:
$t = $garages->children('s', TRUE)->Body->GetListOfFittingCentresResponse->GetListOfFittingCentresResult->children('a', TRUE)->FittingCentres->children('b', TRUE)->FittingCentre;
foreach ($t as $garage) {
echo $garage->Address1."<br />";
}
and 和
echo $garages->GetListOfFittingCentresResponse->GetListOfFittingCentresResult->FittingCentres->FittingCentre[0]->Address1;
Both just throw faults, and I'm afraid I'm out of ideas. 两者都只是错误,我恐怕没有主意了。
Please can someone suggest where I'm being an idiot. 请有人提出我在白痴的位置。 Thanks :)
谢谢 :)
$t = $garages->children('s', TRUE)->Body->GetListOfFittingCentresResponse ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This does not work because that Body
element does not have a child-element named GetListOfFittingCentresResponse
in it's own namespace. 这是行不通的,因为该
Body
元素在其自己的命名空间中没有名为GetListOfFittingCentresResponse
的子元素。 Instead you need to obtain all children in the default namespace to obtain it: 相反,您需要获取默认名称空间中的所有子级才能获取它:
$xml->children('s', 1)->Body->children()->GetListOfFittingCentresResponse
^^^^^^^^^^^^
You need to do this correctly for the whole traversal. 您需要在整个遍历中正确执行此操作。
How to access element with simplexml not in the default document namespace is explained here: 此处说明如何使用simplexml不在默认文档名称空间中的元素进行访问:
This is also sort of the reference question for that kind of questions. 这也是这类问题的参考问题。
A better option often is to use Xpath for traversal: 更好的选择通常是使用Xpath进行遍历:
$xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('n', 'http://TyreMen.com/UkTyreNetwork/');
$xml->xpath('/*/s:Body/n:GetListOfFittingCentresResponse');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.