Hi all,
I'm new to this magicparser. following is my xml tag and theres repeating tag. How can I print each "Facility" in a loop.
<Facilities>
<Facility>24-hour reception</Facility>
<Facility>Baby Sitting</Facility>
<Facility>Balcony</Facility>
<Facility>Barbeque</Facility>
<Facility>Bicycles Available</Facility>
<Facility>Bushwalking Trails</Facility>
<Facility>DVD Player Hire</Facility>
<Facility>Gourmet Breakfast</Facility>
<Facility>Health Retreat</Facility>
<Facility>Internet Access Available</Facility>
</Facilities>
Hi,
When Magic Parser encounters repeating element names, it resolves subsequent element names by appending @1, @2, etc. to the end of the name. This means that in your record handler, the facilities would each be within records called:
$record["FACILITIES/FACILITY"];
$record["FACILITIES/FACILITY@1"];
$record["FACILITIES/FACILITY@2"];
etc. etc.
The easiest way to display these in a loop (particularly when you are not sure how many there are) is to test for each variable inside a loop, appending @i to the end of the base element name where i is incremented on each iteration. Here is the code you need to use in your record handler, assuming that you have kept the record variable $record as used in the examples:
$i = 0;
while(1)
{
if ($i) $postfix = "@".$i;
if (!isset($record["FACILITIES/FACILITY".$postfix])) break;
print "<p>".$record["FACILITIES/FACILITY".$postfix]."</p>";
$i++;
}
A complete example that shows this working is as follows:
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
/** COPY THIS SECTION OF CODE TO LIST FACILITIES IN YOUR OWN SCRIPT **/
$i = 0;
while(1)
{
if ($i) $postfix = "@".$i;
if (!isset($record["FACILITIES/FACILITY".$postfix])) break;
print "<p>".$record["FACILITIES/FACILITY".$postfix]."</p>";
$i++;
}
/** END SECTION TO COPY **/
}
$xml= "<hotels>
<hotel>
<Facilities>
<Facility>24-hour reception</Facility>
<Facility>Baby Sitting</Facility>
<Facility>Balcony</Facility>
<Facility>Barbeque</Facility>
<Facility>Bicycles Available</Facility>
<Facility>Bushwalking Trails</Facility>
<Facility>DVD Player Hire</Facility>
<Facility>Gourmet Breakfast</Facility>
<Facility>Health Retreat</Facility>
<Facility>Internet Access Available</Facility>
</Facilities>
</hotel>
</hotels>";
MagicParser_parse("string://".$xml,"myRecordHandler","xml|HOTELS/HOTEL/");
?>
Hope this helps!
Cheers,
David.