note 41454 deleted from ref.xml by didou

From: Date: Thu, 03 Feb 2005 19:46:16 +0000
Subject: note 41454 deleted from ref.xml by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-84461@lists.php.net to get a copy of this message
Note Submitter: info at b1g dot de ---- Use this class to parse RSS/RDF Feeds. You will get an array with Title and Link of every item. The array will look like this: Array ( [1] => Array ( [link] => http://..... [titel] => Title of Item 1 ) [2] => Array ( [link] => http://... [titel] => Title of Item 2 ) ....... ) Here is the code: <?php class RDFParser { var $_item; var $_url; function RDFParser($url) { $this->_url = $url; } function ParseRDF() { $this->_item = array('i' => 0); $parser = xml_parser_create(); xml_set_object($parser, &$this); xml_set_element_handler($parser, "_startElement", "_endElement"); xml_set_character_data_handler($parser, "_charHandler"); $fp = fopen($this->_url, "r"); while(!feof($fp)) { $line = fgets($fp, 4096); xml_parse($parser, $line); } fclose($fp); xml_parser_free($parser); return($this->_item['items']); } function _startElement($parser, $name, $attrs) { $this->_item['maychar'] = true; if($name=="ITEM") { $this->_item['mayparse'] = true; $this->_item['i']++; } elseif($name=="TITLE") { $this->_item['akt'] = "TITLE"; } elseif($name=="LINK") { $this->_item['akt'] = "LINK"; } else { $this->_item['maychar'] = false; } } function _endElement($parser, $name) { if($name=="ITEM") { $this->_item['mayparse'] = false; } elseif($name=="TITLE" || $name=="LINK") { $this->_item['maychar'] = false; } } function _charHandler($parser, $data) { if($this->_item['maychar'] && $this->_item['mayparse']) { if($this->_item['akt']=="TITLE") { $this->_item['items'][$this->_item['i']]['titel'] = $data; } if($this->_item['akt']=="LINK") { $this->_item['items'][$this->_item['i']]['link'] = $data; } } } } $rdf =& new RDFParser("http://www.tagesschau.de/newsticker.rdf"); print_r($rdf->ParseRDF()); ?> Dirty code, I know, but it works fine for me.

« previous php.notes (#84461) next »