On Thu, Apr 30, 2009 at 5:02 AM, AngeloZanetti <angelo...@gmail.com> wrote:
>
> Hi all,
>
> I have the following XML returned to us.
>
> <Response ResponseReference="REF_657-17000-305754723972091">
>        <ResponseDetails Language="en">
>                <SearchChargeConditionsResponse>
>                        <ChargeConditions>
>                                <ChargeCondition Type="cancellation">
>                                        <Condition Charge="true" FromDay="0" 
> ToDay="0" Currency="ZAR"
> ChargeAmount="320.00"/>
>                                        <Condition Charge="false" FromDay="1"/>
>                                </ChargeCondition>
>                                <ChargeCondition Type="amendment" 
> MaximumPossibleChargesShown="true">
>                                        <Condition Charge="false" FromDay="0"/>
>                                </ChargeCondition>
>                                <PassengerNameChange Allowable="true"/>
>                        </ChargeConditions>
>                </SearchChargeConditionsResponse>
>        </ResponseDetails>
> </Response>
>
> We need to get the values of the FromDay, ToDay and ChargeAmount from the
> following sub elements:
>
> <Condition Charge="true" FromDay="0" ToDay="0" Currency="ZAR"
> ChargeAmount="320.00"/>
> <Condition Charge="false" FromDay="1"/>
>
> I hve the following code: But I am not able to get the second element's
> details: (Charge: false, FromDay: 1)
>
> $Elements = $xpath->query(
> 'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/ChargeCondition/',
> $responseElement );
>
>
>                                foreach( $Elements as $Element )
>                                {
>
>                                        $itemStatus = 
> $xpath->query('Condition/Charge' , $Element);
>                                        $FromDay2 = 
> $xpath->query('Condition/FromDay' , $Element);
>
>                                        $condition = 
> trim($itemStatus->item(0)->textContent);
>                                        echo "<br /><br />CONDITION: #" . 
> $condition."#";
>
>                                        $FromDay = 
> trim($FromDay2->item(0)->textContent);
>                                        echo " FromDay: " . $FromDay;
>
>                                        if ($condition =="true")
>                                        {
>                                                $ChargeAmount2 = 
> $xpath->query('Condition/@ChargeAmount' , $Element);
>                                                $ChargeAmount = 
> trim($ChargeAmount2->item(0)->textContent);
>                                                echo "  CHARGE AMT: " . 
> $ChargeAmount;
>
>                                                $ToDay = 
> $xpath->query('Condition/@ToDay' , $Element);
>                                                $ToDay = 
> trim($ToDay->item(0)->textContent);
>                                                echo " ToDay: " . $ToDay;
>
>                                        }
>                                        else if($condition =="false")
>                                        {
>                                                $FromDay2 = 
> $xpath->query('Condition/@FromDay' , $Element);
>                                                $FromDay = 
> trim($FromDay2->item(0)->textContent);
>                                                echo " FromDay: " . $FromDay;
>                                        }
>                                }
>
> It apprears that I need to add a further loop through the "ChargeCondition"
> elements, IE
>
> change
>
> $Elements = $xpath->query(
> 'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/ChargeCondition/',
> $responseElement );
>
>
> to
>
> $Elements = $xpath->query(
> 'ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/',
> $responseElement );
>
> Then I can test the type if its "cancellation" and loop through the
> subelements (Condition)
>
> Would this appear to be correct?
>
> Anything else that would appear to be incorrect from the above code for the
> accompanying structure?
>
> thanks
> angelo
>

You might be able to cut down your code further with something like this:

<?php

$Elements = $xpath->query(
"ResponseDetails/SearchChargeConditionsResponse/ChargeConditions/chargeconditi...@type='cancellation']/Condition",
$responseElement );


foreach( $Elements as $Element )
{

    $condition = $Element->getAttribute('Charge');
    $FromDay = $Element->getAttribute('FromDay');

    echo "<br /><br />CONDITION: #" . $condition."#";

    echo " FromDay: " . $FromDay;

    if ($condition =="true")
    {
           $ChargeAmount = $Element->getAttribute('ChargeAmount');
           echo "  CHARGE AMT: " . $ChargeAmount;

           $ToDay = $Element->getAttribute('ToDay');
           echo " ToDay: " . $ToDay;

    }
    else if($condition =="false")
    {
           echo " FromDay: " . $FromDay;
    }
}

?>

I'm not sure if you really want it to print FromDay twice in the case
that Charge == 'false', but you should be able to sort that out.

Andrew

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to