Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xml Parser for PHP site

Status
Not open for further replies.

crounauer

Technical User
Feb 7, 2006
1
GB
Hi,

I was wondering if anyone could help with this xml parser. I have got as far as I can, but just can't fugure out why its not working.

Thanks,
Simon.

<?php

$insideitem = false;
$tag = "";
$hotel_name = "";
$hotel_address = "";
$hotel_link = "";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $hotel_name, $hotel_address, $hotel_link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "HOTEL") {
$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $hotel_name, $hotel_address, $hotel_link;
if ($name == "HOTEL") {
printf("<dt><b><a href='%s'>%s</ trim($link),htmlspecialchars(trim($title)));
printf("<dd>%s</dd>",htmlspecialchars(trim($description)));
$hotel_name = "";
$hotel_address = "";
$hotel_link = "";
$insideitem = false;
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $hotel_name, $hotel_address, $hotel_link;
if ($insideitem) {
switch ($tag) {
case "HOTEL_NAME":
$hotel_name .= $data;
break;
case "HOTEL_ADDRESS":
$hotel_address .= $data;
break;
case "HOTEL_LINK":
$hotel_link .= $data;
break;
}
}
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen(" or die("Error reading XML data.");
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);

?>

<?xml version="1.0" encoding="ISO-8859-1" ?>
<hotel_search>
<hotel>
<hotel_name>Park Plaza Leeds</hotel_name>
<hotel_address>Boar Lane City Square</hotel_address>
<hotel_link> </hotel>
</hotel_search>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top