You are here:  » retrive date with same name


retrive date with same name

Submitted by vjeko11 on Tue, 2015-10-27 15:15 in

Hi I have one question

this is XML and php function

{code saved}

(it is work)

But I have problem to retrive this data from xml

Hotel Collect Booking Collect Payment From Guest
Non-Smoking
1 bed

do you have solution?

Thanks!!!

{code saved}

Submitted by support on Tue, 2015-10-27 15:42

Hello vjeko,

In this case, I think it would be best to parse at the BOOKINGRETRIEVALRS/BOOKINGS/BOOKING/ level - that way, you can them use the iteration pattern to extract any of the repeating child elements required. Consider the following example (with your example XML saved as bookings.xml)...

<?php
  
require("MagicParser.php");
  
$data file_get_contents("bookings.xml");
  
MagicParser_parse("string://".$data,"myRecordHandler","xml|BOOKINGRETRIEVALRS/BOOKINGS/BOOKING/");
  function 
myRecordHandler($record)
  {
    global 
$bookingDetails;
    
$bookingDetail = array();
    
$bookingDetail["BOOKING-ID"] = $record["BOOKING-ID"];
    
$bookingDetail["HOTEL-ID"] = $record["HOTEL-ID"];
    
$specialRequests = array();
    
$i "";
    
$p "";
    while(
1)
    {
      if (
$i$p "@".$i;
      if (!isset(
$record["SPECIALREQUEST".$p."-CODE"])) break;
      
$code $record["SPECIALREQUEST".$p."-CODE"];
      
$text $record["SPECIALREQUEST".$p];
      
$specialRequests[$code] = $text;
      
$i++;
    }
    
$bookingDetail["SPECIALREQUESTS"] = $specialRequests;
    
$bookingDetails[] = $bookingDetail;
  }
  
print_r($bookingDetails);
?>

The output being:

Array
(
    [0] => Array
        (
            [BOOKING-ID] => xx
            [HOTEL-ID] => xx
            [SPECIALREQUESTS] => Array
                (
                    [1.14] => 1 king bed
                    [2.1] => Non-Smoking
                    [5] => Hotel Collect Booking Collect Payment From Guest
                )
        )
    [1] => Array
        (
            [BOOKING-ID] => xx
            [HOTEL-ID] => xx
            [SPECIALREQUESTS] => Array
                (
                    [5] => Hotel Collect Booking Collect Payment From Guest
                    [2.1] => Non-Smoking
                    [1.40] => 1 bed
                )
        )
)

Hope this helps!
Cheers,
David
--
MagicParser.com

Submitted by vjeko11 on Tue, 2015-10-27 17:43

Thank, works perfectly :)