You are here:  » Parsing XML for MultiValues


Parsing XML for MultiValues

Submitted by majid on Thu, 2008-08-07 05:05 in

Hi

My sample xml file

http://www.essoog.com/maps/Sample.xml

I am using

$request1 = " MagicParser_parse("string://".$response,"myRecordHandler1","xml|HOTELROOMAVAILABILITYRESULTS/HOTELROOMAVAILABILITYRESULT/");

myRecordHandler1 gets me information on hotelroomavailability tags, so i need it.

samplecode from myRecordHandler1. In this, I would need the displayNightlyRate and nativeNightlyRate tag info in comma seperated values, so for the sample xml file, i would need "176.93, 176.93" for the displayNightlyRate tag as there are two of them. Hope this makes sense.

            <input type="hidden" name="hotel_taxrate" value="'.$result["TAXRATE"].'" />
            <input type="hidden" name="hotel_bedtype" value="'.$result["BEDTYPES/BEDTYPE/ID"].'" />
            <input type="hidden" name="hotel_chargeableroomrate" value="'.$result["RATEINFO/CHARGEABLEROOMRATETOTAL"].'" />
            <input type="hidden" id="hotel_roomTypeCode" name="hotel_roomTypeCode" value="'. $result["ROOMTYPECODE"] .'" />
            <input type="hidden" name="hotel_roomTypeDescription" value="'.$result["ROOMTYPEDESCRIPTION"].'" />
            <input type="hidden" name="hotel_rateCode" value="'. $result["RATECODE"] .'" />
            <input type="hidden" name="hotel_rateDescription" value="'.$result["RATEDESCRIPTION"].'" />
            <input type="hidden" name="hotel_hrnQuoteKey" value="'.$result["HRNQUOTEKEY"].'" />
            <input type="hidden" name="hotel_rateChange" value="'.$result["RATECHANGE"].'" />
            <input type="hidden" name="hotel_propertyId" value="'.$result["PROPERTYID"].'" />
            <input type="hidden" name="hotel_cancellationPolicy" value="'.$result["CANCELLATIONPOLICY"].'" />
            <input type="hidden" name="hotel_supplierType" value="'.$result["SUPPLIERTYPE"].'" />
            <input type="hidden" name="hotel_propertyType" value="'.$result["PROPERTYTYPE"].'" />
            <input type="hidden" name="hotel_displayRoomRate" value="'.$result["RATEINFO/DISPLAYROOMRATE"].'" />
            <input type="hidden" name="hotel_displayNightlyRate" value="'.$result["RATEINFO/DISPLAYNIGHTLYRATES/DISPLAYNIGHTLYRATE"].'" />
            <input type="hidden" name="hotel_displayCurrencyCode" value="'.$result["RATEINFO/DISPLAYCURRENCYCODE"].'" />
            <input type="hidden" name="hotel_nativeRoomRate" value="'.$result["RATEINFO/NATIVEROOMRATE"].'" />
            <input type="hidden" name="hotel_nativeNightlyRate" value="'.$result["RATEINFO/NATIVENIGHTLYRATES/NATIVENIGHTLYRATE"].'" />
            <input type="hidden" name="hotel_nativeCurrencyCode" value="'.$result["RATEINFO/NATIVECURRENCYCODE"].'" />
            <input type="hidden" name="hotel_rateFrequency" value="'.$result["RATEINFO/RATEFREQUENCY"].'" />
            <input type="hidden" name="hotel_chargeableRoomRateTaxesAndFees" value="'.$result["RATEINFO/CHARGEABLEROOMRATETAXESANDFEES"].'" />
            <input type="hidden" name="hotel_chargeableRoomRateTotal" value="'.$result["RATEINFO/CHARGEABLEROOMRATETOTAL"].'" />

Please help to read this nightly rate multi values.

Regards,
Majid

Submitted by support on Thu, 2008-08-07 07:33

Hello majid,

To do this, the easiest way is to construct your displaynightlyrate and nativenightlyrate values before you start printing the form, and then use the variable created rather than writing directly from $record.

As you will have noticed, Magic Parser has resolved the duplicate field names using @1 (any further duplicates of the same XML tag would have @2, @3 etc.) appended. There are various ways to handle this, but in this case, I think the following code is the most efficient, so add this to your myRecordHandler function before the example code you posted above:

// create arrays to hold each of the values
$displaynightlyrates = array();
$nativenightlyrates = array();
foreach($record as $k => $v)
{
  // loop through each field, if it is a display or native nightly rate add the value ($v) to the array
  if (strpos($k,"RATEINFO/DISPLAYNIGHTLYRATES/DISPLAYNIGHTLYRATE")===0)
  {
    $displaynightlyrates[] = $v;
  }
  if (strpos($k,"RATEINFO/NATIVENIGHTLYRATES/NATIVENIGHTLYRATE")===0)
  {
    $nativenightlyrates[] = $v;
  }
}
// construct the CSV value by imploding the values in each array with a comma
$displaynightlyrate = implode(",",$displaynightlyrates);
$nativenightlyrate = implode(",",$nativenightlyrates);

Now, you can use $displaynightlyrate and $nativenightlyrate in your form code, for example:

<input type="hidden" name="hotel_displayNightlyRate" value="'.$displaynightlyrate.'" />

...and:

<input type="hidden" name="hotel_nativeNightlyRate" value="'.$nativenightlyrate.'" />

Hope this helps!
Cheers,
David.