Welcome to WebmasterWorld Guest from 107.22.87.205

Forum Moderators: bill & werty

Message Too Old, No Replies

XML Parsing

Can anyone find the fault with this code?

     
4:47 pm on Feb 16, 2006 (gmt 0)

10+ Year Member



Hi,

I have done the script below and am trying to parse a feed from Laterooms. It is showing the county ID, but not the country name. Any suggestion?

Thanks,
Simon

<code>
<?php
$insideitem = false;
$tag = "";
$country_code = "";
$country_name = "";

function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $country_code, $country_name;
if ($insideitem) {
$tag = $name;
} elseif ($name == "COUNTRY") {
$insideitem = true;
}
}

function endElement($parser, $name) {
global $insideitem, $tag, $country_code, $country_name;
print "". $country_code ."&nbsp;";
print "". $country_name ."<br>";
$country_code = "";
$country_name = "";
$insideitem = false;
}

print "<h2>COUNTRIES</h2>";

function characterData($parser, $data) {
global $insideitem, $tag, $country_code, $country_name;
if ($insideitem) {
switch ($tag) {
case "COUNTRY_CODE":
$country_code .= $data;
break;
case "COUNTRY_NAME":
$country_name .= $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("http://xml.laterooms.com/xml_ctry.php3?lang=eng","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 2048)) {
xml_parse($xml_parser, $data, feof($fp));
}
fclose($fp);
xml_parser_free($xml_parser);
?>
</code>

 

Featured Threads

Hot Threads This Week

Hot Threads This Month