note 47821 added to function.xml-parse

From: Date: Wed, 01 Dec 2004 18:11:36 +0000
Subject: note 47821 added to function.xml-parse
Groups: php.notes 
Request: Send a blank email to php-notes+get-81230@lists.php.net to get a copy of this message
Here a bit of code to read a RSS 2.0 news feed. It's ugly, 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 = ""; } } } } } } } } } } } } } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47821 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47821&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47821&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47821&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47821&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47821&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47821&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47821&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#81230) next »