I am using simplexml_load_string in order to attempt to turn my XML file into variables I can later enter into a database however am struggling with the output working in some instances and not in others.
The xml
$response ='<ndxml version="2.0">
<status code="OK">
<response function="createNewJob" id="1">
<status code="OK">
<job uniqueref="5830z858279" jobref="858279">
<consignment number="8613030">
<reference>16755</reference>
<deadlinedatetime date="2014-01-16" time="17:30:00">
<jobnumber>
<labeldata styletag="APC">
<shippingdate date="15/01/2014">
<addresses>
<address type="COLLECTION">
<company>UK Stuff and Things</company>
</address>
<address type="DELIVERY">
<contact>Person</contact>
<telephone>02089636985</telephone>
<addresslines>
<addressline number="1">Daffy</addressline>
<addressline number="2">Things</addressline>
<addressline number="3">Places</addressline>
<addressline number="4">NORTHAMPTONSHIRE</addressline>
</addresslines>
<postzip>NB12 1ER</postzip>
<country isocode="GB">United Kingdom</country>
</address>
</addresses>
<notes>
<account code="21171">
<tariff code="MP16">
<routing>
<delivery>
<route>LOCAL</route>
<zone>B</zone>
<driver>31</driver>
<serviceoptions>
</serviceoptions></delivery>
<depots>
<depot number="211" type="Sending">
<depot number="211" type="Request">
<depot number="211" type="Delivery">
</depot></depot></depot></depots>
</routing>
<parcels total="1">
<dimensions height="0" width="0" length="0">
<volweight>0.0</volweight>
<weight>0.14</weight>
<parcel number="1">
<barcode>21163148613030001</barcode>
</parcel>
</dimensions></parcels>
</tariff></account></notes></shippingdate></labeldata>
</jobnumber></deadlinedatetime></consignment></job>
</status>
</response>
So I have managed to successfully grab certain elements from this by using the recommended code on the documentation:
$parsed=simplexml_load_string($response);
$response_statuscode = $parsed->status['code'];
$response_statuscode2 = $parsed->response->status['code'];
$response_consignment_num = $parsed->response->job->consignment['number'];
$response_reference = $parsed->response->job->reference;
All of these have worked exactly as required, however from there it all goes a bit wrong for me. Things with more complicated attributes (more than one!) just don't seem to be working for me.
$response_date = $parsed->response->job->deadlinedatetime['date'];
I also tried:
$parsed->response->job->deadlinedatetime->attributes()->date;
And from there on I can't seem to process anything from label data properly. I am just making a mess of my understanding of the tree?
$response_account_code = $parsed->response->job->labeldata->account['code'];
As always, thanks in advance!