I'm fairly new to PHP and I'm trying to make a simple XML Website News script ("IdleNews"). My first version was exceedingly simple. It basically parsed through the XML file and plopped the data down in between some echo'ed html tags. It added new news items by making a copy of the current xml file called idlenews.cpy, adding on the new entry, deleting the old xml file, and renaming idlenews.cpy to idlenews.xml (I'm sure there would have been an easier way to do this, but I couldn't think of it). This worked great with expat used to parse the xml.
Anyway, I decided to alter it so that a user could edit and delete news entries. I made a simple class that contained an array for each bit of information a single "news item" would contain (index, date, text, poster, and subject). The class contained interface functions to access the data as well as the functions needed to handle opening tags, closing tags, and data in the xml file. Finally, it contains a getNews function that actually parses the xml file and adds the news items to the class' arrays. The getNews function is called from the class constructor. Everything seems to run fine, but I receive the following error:
Warning: Unable to call handler () in d:\program files\web development\apache server\apache\htdocs\newsreader\idlenews3.php on line 170
It comes up about 12 times (each time it tries to run through the loop where the xml file is parsed, I would imagine). I've searched through the code as much as I can, but I can't seem to find the source of the error. The entire sourcecode of the class is provided below, if someone has some insight into what's causing this please let me know (the formatting is probably going to be destroyed in this small window, but I've tried to comment things as much as possible):
// Create a class for individual news items
class newsItems{
// Sets up the class' storage variables
var $newsIndex = array();
var $newsDate = array();
var $newsText = array();
var $newsPoster = array();
var $newsSubject = array();
var $currentItem;
var $currentTag;
var $xml_parser;
function newsItems($theFile){
$this->getNews($theFile);
}
function getTotal(){
return count($newsIndex);
}
function getDate($theIndex){
return $this->newsDate[$theIndex];
}
function getText($theIndex){
return $this->newsText[$theIndex];
}
function getPoster($theIndex){
return $this->newsPoster[$theIndex];
}
function getSubject($theIndex){
return $this->newsSubject[$theIndex];
}
// Handles the opening tags in the xml file and increments the current item in the database
function openTag($parser, $name, $attrs){
$this->currentTag = $name;
switch ($name){
case "NEWSITEM":
++$currentItem;
$this->newsIndex[$this->currentItem] = $currentItem;
break;
default:
break;
}
}
// Handles the closing xml tags, takes no action
function closeTag($parser, $name){
}
// Reads in the data between xml tags and puts it in the database
function xmlData($parser, $data){
switch ($this->currentTag){
case "DATE":
$this->newsDate[$this->currentItem] = $data;
break;
case "TEXT":
$this->newsText[$this->currentItem] = $data;
break;
case "SUBJECT":
$this->newsSubject[$this->currentItem] = $data;
break;
default:
break;
}
}
function getNews($idleFile){
// Create XML parser
$this->xml_parser = xml_parser_create();
// Set callback functions
xml_set_element_handler($this->xml_parser, "$this->openTag", "$this->closeTag");
xml_set_character_data_handler($this->xml_parser, "$this->xmlData");
// open XML file
if (!($fp = fopen($idleFile, "r")))
{
die("Cannot locate XML data file: $idleFile");
}
// read and parse data
while ($data = fread($fp, 4096))
{
// error handler
if (!xml_parse($this->xml_parser, $data, feof($fp)))
{
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser)));
}
}
// clean up
xml_parser_free($this->xml_parser);
}
}