Hi,
I’m having trouble getting the XML parser to handle the “&# 8217” single-quote character from an RSS feed. Here is my code (basically taken from the Intermediate XML Parsing tutorial here)
<?php
$xml_file = "feed.xml";
$xml_headline_key = "*RSS*CHANNEL*ITEM*TITLE";
$xml_link_key = "*RSS*CHANNEL*ITEM*LINK";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $link;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_headline_key, $xml_link_key, $counter, $story_array;
switch($current_tag){
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
break;
case $xml_link_key:
$story_array[$counter]->link = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
?>
<?
for($x=0;$x<count($story_array);$x++){
$link=$story_array[$x]->link;
$headline=$story_array[$x]->headline;
echo "<li><a href=$link>$headline</a></li>
";
}
Instead of properly displaying the quote mark, it cuts off everything before the quote mark in the current line. Any thoughts? Please?