PHP中的SimpleXML SOAP响应命名空间问题

PHP中的SimpleXML SOAP响应命名空间问题,php,xml,soap,simplexml,Php,Xml,Soap,Simplexml,我似乎很难理解简单的XML和soap。我已将一个文件读入简单xml,这是显示它已正确读入的结果: echo $garages->asXML(); // result <?xml version="1.0"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <GetListOfFittingCentresResponse xmln

我似乎很难理解简单的XML和soap。我已将一个文件读入简单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>
两人都有缺点,恐怕我已经没有主意了

有人能告诉我我在哪里做白痴吗。谢谢:)

这不起作用,因为
Body
元素在其自己的命名空间中没有名为
GetListofSettings CentresResponse
的子元素。相反,您需要获取默认命名空间中的所有子项才能获取它:

$xml->children('s', 1)->Body->children()->GetListOfFittingCentresResponse
                            ^^^^^^^^^^^^
您需要在整个遍历过程中正确执行此操作

下面解释了如何使用不在默认文档命名空间中的simplexml访问元素:

这也是这类问题的参考问题

更好的选择通常是使用Xpath进行遍历:

$xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('n', 'http://TyreMen.com/UkTyreNetwork/');

$xml->xpath('/*/s:Body/n:GetListOfFittingCentresResponse');

谢谢你的回答。我仍然很迷茫,但今天晚些时候我会努力想清楚:)@Jon:我试着让它更容易理解。您需要想象每个元素都有一个名称空间。
$t = $garages->children('s', TRUE)->Body->GetListOfFittingCentresResponse ...
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$xml->children('s', 1)->Body->children()->GetListOfFittingCentresResponse
                            ^^^^^^^^^^^^
$xml->registerXPathNamespace('s', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('n', 'http://TyreMen.com/UkTyreNetwork/');

$xml->xpath('/*/s:Body/n:GetListOfFittingCentresResponse');