1

I'm having a seemingly impossible time getting my head around simple XML and soap. I've read in a file to simple xml, and this is the result to show it's read in properly:

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 :)

Jon
  • 155
  • 7

1 Answers1

1
$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. 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:

This is also sort of the reference question for that kind of questions.

A better option often is to use Xpath for traversal:

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

$xml->xpath('/*/s:Body/n:GetListOfFittingCentresResponse');
Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Thank you for answering. I'm still fairly lost but I'll try and get my head around this later today :) – Jon May 08 '14 at 11:45
  • @Jon: I tried to make it a bit better to understand. You need to imagine that each element has a namespace. – hakre May 08 '14 at 11:57