I've been trying to retro-fit sample code found at : http://
www.sitepoint.com/article/php-xml-parsing-rss-1-0 to parse XML coming
from a server.
I'm sooo close to getting this working, but I've hit a roadblock and
could use some help.
To see the data I'm needing to parse, go here: http://
www.librarytools.com/events/sampledata.txt. This is just sample data
from the host, as it's behind a firewall.
The problem I'm having is that the XML data that comes back from the
host doesn't just have <eventname> </eventname> tags. It has <f
n="eventname">datahere</f> tags, and I don't know how to get the XML
parser to read the values using that format. (And I don't have
control over the host) As a first step, I just want to retrieve the
"eventname", "venuename", and "venuecity" data from within the
"result" tags at the bottom of the data.
I'm really hoping this is possible with PHP - - - can someone please
steer me in the right direction and tell my what I should change in
the below script? The $tag value is not getting a value string, due
to the XML-data....
Here's my present script:
<?php
$insideitem = false;
$tag = "";
$eventname = "";
$venuename = "";
$venuecity = "";
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $eventname, $venuename, $venuecity;
if ($insideitem) {
$tag = $name;
} elseif ($name == "RESULT") {
echo "found result";
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $eventname, $venuename, $venuecity;
if ($name == "RESULT") {
echo "result in endElement";
printf("<dt><b><a href='%s'>%s</a></b></dt>",
trim($venuecity),htmlspecialchars(trim($eventname)));
printf("<dd>%s</dd>",htmlspecialchars(trim($venuename)));
$eventname = "";
$venuename = "";
$venuecity = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $eventname, $venuename, $venuecity;
if ($insideitem) {
echo $tag;
switch ($tag) {
case "EVENTNAME":
$eventname .= $data;
echo "got an event!";
break;
case "VENUENAME":
$venuename .= $data;
break;
case "VENUECITY":
$venuecity .= $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://www.librarytools.com/events/sampledata.txt","r")
or die("Error reading RSS data.");
echo $fp;
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);
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php