note 58568 added to function.xml-parse-into-struct

From: Date: Tue, 08 Nov 2005 11:48:26 +0000
Subject: note 58568 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-98247@lists.php.net to get a copy of this message
Searching for a nice and working way to get a RSS feed into an array-structure, I found the solution posted by kieran but disliked those several eval()s. So I wrote my own using references/pointers. <?php class RSSParser { var $struct = array(); // holds final structure var $curptr; // current branch on $struct var $parents = array(); // parent branches of current branch function RSSParser($url) { $this->curptr =& $this->struct; // set ref to base $xmlparser = xml_parser_create(); xml_set_object($xmlparser, $this); xml_set_element_handler($xmlparser, 'tag_open', 'tag_close'); xml_set_character_data_handler($xmlparser, 'cdata'); $fp = fopen($url, 'r'); while ($data = fread($fp, 4096)) xml_parse($xmlparser, $data, feof($fp)) || die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xmlparser)), xml_get_current_line_number($xmlparser))); fclose($fp); xml_parser_free($xmlparser); } function tag_open($parser, $tag, $attr) { $i = count($this->curptr['children']); $j = count($this->parents); $this->curptr['children'][$i]=array(); // add new child element $this->parents[$j] =& $this->curptr; // store current position as parent $this->curptr =& $this->curptr['children'][$i]; // submerge to newly created child element $this->curptr['name'] = $tag; if (count($attr)>0) $this->curptr['attr'] = $attr; } function tag_close($parser, $tag) { $i = count($this->parents); if ($i>0) $this->curptr =& $this->parents[$i-1]; // return to parent element unset($this->parents[$i-1]); // clear from list of parents } function cdata($parser, $data) { $data = trim($data); if (!empty($data)) { $this->curptr['value'] .= $data; } } } $myparser = new RSSParser('getitems.xml'); $anotherparser = new RSSParser('http://johndoe:secret@myfeeds.com/getfeed.xml'); print_r($myparser->struct); print_r($anotherparser->struct); ?> ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58568 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58568&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58568&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58568&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58568&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58568&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58568&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58568&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58568&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98247) next »