Parsing response from web service

Junior Programmer here!

calling a web service and using simplexml_load_string to build the response. I now need to parse the data and display it back to user in a table using HTML. for some reson some of the data wont display.

I am unable to get data for the following items “delivery-options:” and “significant-event”

See my results:

customer-ref-1:blah
customer-ref-2:
return-pin:
signature-image-exists:true
suppress-signature:false
delivery-options: <---- EMPTY
significant-events: <---- EMPTY

Here is the structure when printing the content of the response

[signature-image-exists] => true
[suppress-signature] => false
[delivery-options] => SimpleXMLElement Object
(
[item] => SimpleXMLElement Object
(
[delivery-option] => CH_SGN_OPTION
[delivery-option-description] => Signature Required
)

    )

[significant-events] => SimpleXMLElement Object
    (
        [occurrence] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [event-identifier] => 1496
                        [event-date] => 2011-02-03
                        [event-time] => 11:59:59
                        [event-time-zone] => EST
                        [event-description] => Item received
                        [signatory-name] => SimpleXMLElement Object
                            (
                            )

                        [event-site] => Canada
                        [event-province] => QC
                        [event-retail-location-id] => SimpleXMLElement Object
                            (
                            )

                        [event-retail-name] => SimpleXMLElement Object
                            (
                            )

                    )

                [1] => SimpleXMLElement Object
                    (
                        [event-identifier] => 20
                        [event-date] => 2011-02-03
                        [event-time] => 11:59:59
                        [event-time-zone] => EST
                        [event-description] => Return pkg to client
                        [signatory-name] => HETU
                        [event-site] => SAINTE-FOY
                        [event-province] => QC
                        [event-retail-location-id] => SimpleXMLElement Object
                            (
                            )

                        [event-retail-name] => SimpleXMLElement Object
                            (
                            )

                    )

My code snippet

[php]libxml_use_internal_errors(true);
$xml = simplexml_load_string($curl_response);
if (!$xml) {
echo “Failed loading XML\n”;
echo “$curl_response\n”;
foreach (libxml_get_errors() as $error) {
echo “\t” . $error->message;
}
} else {

$trackingDetail = $xml->children('http://www.blah.com/ws/track');
if ($trackingDetail->{"pin"}) {
    //echo "PIN Number: " . $trackingDetail->{"pin"} . "\n";
    foreach ($xml->children() as $child) {
        echo $child->getName() . ":" . $child . "<br />";

    }
} else {
    $messages = $xml->children('http://www.blah.com/ws/messages');
    foreach ($messages as $message) {
        echo "Error Code: " . $message->code . "\n";
        echo "Error Msg: " . $message->description . "\n\n";
    }
}

}[/php]

I think I am not using the foreach proprely …but yet again…

Thank You

As the delivery-options and significant-events are objects in themselves, it may be required to loop through them separately and product the output.

How would you want them to appear? A deliver-options line for each delivery option? Or multiple ones on the same line?

Sponsor our Newsletter | Privacy Policy | Terms of Service