Hello david, hope you are at the best of health!
Got a small problem here..
I am trying to parse data of a dynamicly generated xml file (web service). It is basically a hotel room availability service. i would pass on the hotel_id, arrival_date and departure_date.
And the output would be something like the code below. (would vary depending on rooms available at the dates required)
The output records required are:
room_id
date
price
price_id
this is what I am getting at the moment:
DATE: 2007-10-10
PRICE: 140.00
RATE ID: 27073
ROOM ID:
DATE: 2007-10-10
PRICE: 155.00
RATE ID: 27073
ROOM ID:
DATE: 2007-10-10
PRICE: 110.00
RATE ID: 27073
ROOM ID:
DATE: 2007-10-10
PRICE: 79.00
RATE ID: 27073
ROOM ID:
DATE: 2007-10-10
PRICE: 110.00
RATE ID: 27073
ROOM ID:
As you can see, the room id is missing, as well as the rest of the dates. (only the first date is showing)
{code saved}
and this is the php code:
function getRoomAvailability($record)
{
echo "DATE: " . $record["DESCRIPTION/ANON/DATE"] . "<br>";
echo "PRICE: " . $record["DESCRIPTION/ANON/PRICE"] . "<br>";
echo "RATE ID: " . $record["DESCRIPTION/ANON/RATE_ID"] . "<br>";
echo "ROOM ID: " . $record["DESCRIPTION/ROOM_ID"] . "<br><BR><BR>";
}
MagicParser_parse($url,"getRoomAvailability", "xml|GETROOMAVAILABILITY/RESULT/");
Hi,
First things first - the only reason you are not seeing ROOM ID is because you only need to use $record["ROOM_ID"], not $record["DESCRIPTION/ROOM_ID"]. The other issue is occurring because the XML contains more than one ANON record - presumably one per night of the requested dates. To access these variables, you have to use the fact that Magic Parser resolves duplicate element names by appending @1, @2 etc. to the end of the name for subsequent duplicate names. In your record handler, the easiest way to handle this is to write a loop to increment a counter, and then look for elements of the base name + the postfix, which is @+the counter.
I've written an example to help you out here; here is the output running on this server:
http://www.magicparser.com/examples/availability.php
Here's the source:
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
echo "ROOM ID:".$record["ROOM_ID"];
echo "<blockquote>";
$i = 0;
while(1) {
if ($i) $postfix = "@".$i;
if (!isset($record["DESCRIPTION/ANON".$postfix])) break;
$date = $record["DESCRIPTION/ANON/DATE".$postfix];
$price = $record["DESCRIPTION/ANON/PRICE".$postfix];
$rate_id = $record["DESCRIPTION/ANON/RATE_ID".$postfix];
echo "DATE: ".$date."<br>";
echo "PRICE: ".$price."<br>";
echo "RATE ID: ".$rate_id."<br>";
echo "<br>";
$i++;
}
echo "</blockquote>";
}
MagicParser_parse("availability.xml","myRecordHandler","xml|GETROOMAVAILABILITY/RESULT/");
?>
Hope this helps!
Cheers,
David.