note 47820 deleted from function.xml-parse by irchtml

From: Date: Sat, 08 Jan 2005 00:49:17 +0000
Subject: note 47820 deleted from function.xml-parse by irchtml
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-82940@lists.php.net to get a copy of this message
Note Submitter: brodseba at brodseba dot com ---- Here a code to retrieve RSS 2.0 Specs news feed. It's ungly, but it work. <?php $file = 'http://rss.pcworld.com/rss/latestnews.rss'; $stack = array(); function startTag($parser, $name, $attrs) { global $stack; $tag=array("name"=>$name,"attrs"=>$attrs); array_push($stack,$tag); } function cdata($parser, $cdata) { global $stack,$i; if(trim($cdata)) { $stack[count($stack)-1]['cdata']=$cdata; } } function endTag($parser, $name) { global $stack; $stack[count($stack)-2]['children'][] = $stack[count($stack)-1]; array_pop($stack); } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startTag", "endTag"); xml_set_character_data_handler($xml_parser, "cdata"); $data = xml_parse($xml_parser,file_get_contents($file)); if(!$data) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } xml_parser_free($xml_parser); $curElement = ""; $curTitle = ""; $curLink = ""; $curDesc = ""; foreach($stack as $news) { foreach($news as $new) { if(is_array($new)) { foreach($new as $ne) { if(is_array($ne)) { foreach($ne as $channel) { if(is_array($channel)) { foreach($channel as $news) { foreach($news as $new) { if(is_array($new)) { foreach($new as $ne) { foreach($ne as $n) { //echo $n; if($n == "TITLE") { $curElement = "TITLE"; } elseif($n == "DESCRIPTION") { $curElement = "DESCRIPTION"; } elseif($n == "LINK") { $curElement = "LINK"; } elseif(!is_array($n)) { if($curElement == "TITLE") { if($curTitle != "") { echo "<a href='$curLink'><p><strong>$curTitle</strong></a><br>\n"; echo "$curDesc</p>"; } $curTitle = $n; $curElement = ""; } if($curElement == "LINK") { $curLink = $n; $curElement = ""; } if($curElement == "DESCRIPTION") { $curDesc = $n; $curElement = ""; } } } } } } } } } } } } } } ?>

« previous php.notes (#82940) next »