I was tweaking the script to read multiple RSS.
It works fine, when I create a new RssParser object I use it and destroy it and create another RssParser to read another RSS doc which I dont want to do.
Instead I want to open one RssParser object read a RSS doc and change the source and read again.
It does not happen even when I freed the file pointer.
link: http://raghavan20.allhyper.com/xmlParser.php for errors
errors come differently depending how I switch the stmts in the last code snippet
This works fine
Code: Select all
<?
class RSSParser {
var $insideitem = false;
var $tag = "";
var $title = "";
var $description = "";
var $link = "";
var $itemCount = 5;
var $counter = 0;
var $source = "";
var $parser;
var $fp;
var $values = array();
function RSSParser($source){
$this->parser = xml_parser_create(); //create a xml parser
xml_set_object($this->parser, &$this);
xml_set_element_handler($this->parser, "startElement", "endElement"); //tags to be processed
xml_set_character_data_handler($this->parser, "characterData"); //type of data read
$this->source = $source;
}
function readXML($itemCount){
$this->itemCount = $itemCount; //assign the no of items needed
$this->fp = fopen($this->source,"r") //open a file pointer
or die("Error reading RSS data.");
while ($data = fread($this->fp, 4096)) {//read data from the xml file
xml_parse($this->parser, $data, feof($this->fp)) //call xml_parse which parses xml tags
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->parser)), //error code returned on error
xml_get_current_line_number($this->parser))); //error at line
}
if (!empty($this->values)){
echo "inside readxml return statement block:<br />";
return $this->values;
}
}
function setXMLSource($source){//change the xml source
$this->source = $source;
}
function closeFilePointer(){
fclose($this->fp);
//xml_parser_free($this->parser);
}
function startElement($parser, $tagName, $attrs) {
//echo "Start element:".$parser."<br />".$tagName."<br />";
//print_r($attrs);
if ($this->counter < $this->itemCount){//display only required no of items
if ($this->insideitem) {
$this->tag = $tagName; //inside the ITEM tag and record the name of the tag
} elseif ($tagName == "ITEM") {
$this->insideitem = true; //set true if inside ITEM tag, main tag
}
}else{
return 0;
}
}
function endElement($parser, $tagName) {
//echo "End element:".$parser."<br />".$tagName."<br />";
if ($tagName == "ITEM") {
//copy the values to the array
$index = count($this->values); //find number of elements created and use the next index
if(!empty($this->title) && !empty($this->link) && !empty($this->description) ){
$this->values[$index][0] = $this->title;
$this->values[$index][1] = $this->link;
$this->values[$index][2] = htmlentities($this->description);
print_r($values[$index]);
}
//reset all member variables
$this->title = ""; // all member variables set to null
$this->description = "";
$this->link = "";
$this->insideitem = false;
$this->counter++; //increment the counter as items are read
}
}
function characterData($parser, $data) {
//echo "character data:".$parser."<br />".$data."<br />";
if ($this->insideitem) {
switch ($this->tag) { //store data in appropriate tags for processing
case "TITLE":
$this->title .= $data;
break;
case "DESCRIPTION":
$this->description .= $data;
break;
case "LINK":
$this->link .= $data;
break;
}
}
}
function __destructor(){
fclose($this->fp); //close file pointer
xml_parser_free($this->parser); //return the xml parser
}
}
?>Code: Select all
<?php
//read two items from rediff
$xmlSource = "http://www.rediff.com/rss/newsrss.xml";
$rssParser = new RSSParser($xmlSource); //pass the xml source file location
$values = $rssParser->readXML(2);//read the xml file and display required number of items
echo "array count:".count($values);
if (count($values)){
foreach($values as $list => $listItems){
echo "<div style='width:100%; background-color:black;color:white'>";
echo "<a href = '{$listItems[1]}' style='color:white'>{$listItems[0]}</a></div>";
echo "<div style='width:100%;'>{$listItems[2]}</div>";
/*foreach($listItems as $data){
echo "<div>$data</div>";
}*/
}
}
//$rssParser->closeFilePointer();
unset($rssParser);
//read two items from sitepoint
//$rssParser->setXMLSource("http://www.sitepoint.com/rss.php");
$xmlSource = "http://www.rediff.com/rss/newsrss.xml";
$rssParser = new RSSParser("http://www.sitepoint.com/rss.php"); //pass the xml source file location
$values = $rssParser->readXML(2);//read the xml file and display required number of items
echo "array count:".count($values);
if (count($values)){
foreach($values as $list => $listItems){
echo "<div style='width:100%; background-color:black;color:white'>";
echo "<a href = '{$listItems[1]}' style='color:white'>{$listItems[0]}</a></div>";
echo "<div style='width:100%;'>{$listItems[2]}</div>";
/*foreach($listItems as $data){
echo "<div>$data</div>";
}*/
}
}
unset($rssParser);
?>1. you can see i free the fp, file pointer in a class method
2. i set the new source and call the xmlparse method
Code: Select all
<?php
//read two items from rediff
$xmlSource = "http://www.rediff.com/rss/newsrss.xml";
$rssParser = new RSSParser($xmlSource); //pass the xml source file location
$values = $rssParser->readXML(2);//read the xml file and display required number of items
echo "array count:".count($values);
if (count($values)){
foreach($values as $list => $listItems){
echo "<div style='width:100%; background-color:black;color:white'>";
echo "<a href = '{$listItems[1]}' style='color:white'>{$listItems[0]}</a></div>";
echo "<div style='width:100%;'>{$listItems[2]}</div>";
/*foreach($listItems as $data){
echo "<div>$data</div>";
}*/
}
}
$rssParser->closeFilePointer();
//unset($rssParser);
//read two items from sitepoint
$rssParser->setXMLSource("http://www.sitepoint.com/rss.php");
//$xmlSource = "http://www.rediff.com/rss/newsrss.xml";
//$rssParser = new RSSParser("http://www.sitepoint.com/rss.php"); //pass the xml source file location
$values = $rssParser->readXML(2);//read the xml file and display required number of items
echo "array count:".count($values);
if (count($values)){
foreach($values as $list => $listItems){
echo "<div style='width:100%; background-color:black;color:white'>";
echo "<a href = '{$listItems[1]}' style='color:white'>{$listItems[0]}</a></div>";
echo "<div style='width:100%;'>{$listItems[2]}</div>";
/*foreach($listItems as $data){
echo "<div>$data</div>";
}*/
}
}
unset($rssParser);
?>