cvs: pear /XML_RSS RSS.php package.xml /XML_RSS/tests 01-parse.phpt test.rss

From: Date: Fri, 11 Jan 2002 16:57:29 +0000
Subject: cvs: pear /XML_RSS RSS.php package.xml /XML_RSS/tests 01-parse.phpt test.rss
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-1908@lists.php.net to get a copy of this message
mj Fri Jan 11 11:57:29 2002 EDT Added files: /pear/XML_RSS RSS.php package.xml /pear/XML_RSS/tests 01-parse.phpt test.rss Log: * Initial commit of XML_RSS.

Index: pear/XML_RSS/RSS.php +++ pear/XML_RSS/RSS.php <?php // vim: set expandtab tabstop=4 shiftwidth=4: // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Martin Jansen <mj@php.net> | // | | // +----------------------------------------------------------------------+ // // $Id: RSS.php,v 1.1 2002/01/11 16:57:29 mj Exp $ // require_once "XML/Parser.php"; /** * RSS parser class. * * This class is a parser for Resource Description Framework (RDF) Site * Summary (RSS) documents. For more information on RSS see the * website of the RSS working group (http://www.purl.org/rss/). * * @author Martin Jansen <mj@php.net> * @version $Revision: 1.1 $ * @access public */ class XML_RSS extends XML_Parser { // {{{ properties /** * @var string */ var $insideTag = ""; /** * @var string */ var $activeTag = ""; /** * @var array */ var $channel = array(); /** * @var array */ var $items = array(); /** * @var array */ var $item = array(); /** * @var array */ var $image = array(); /** * @var array */ var $textinput = array(); /** * @var array */ var $textinputs = array(); // }}} // {{{ Constructor /** * Constructor * * @access public * @param mixed File pointer or name of the RDF file. * @return void */ function XML_RSS($handle = "") { $this->XML_Parser(); if (@is_resource($handle)) { $this->setInput($handle); } elseif ($handle != "") { $this->setInputFile($handle); } else { $this->raiseError("No filename passed."); } } // }}} // {{{ startHandler() /** * Start element handler for XML parser * * @access private * @param object XML parser object * @param string XML element * @param array Attributes of XML tag * @return void */ function startHandler($parser, $element, $attribs) { switch ($element) { case "CHANNEL" : case "ITEM" : case "IMAGE" : case "TEXTINPUT" : $this->insideTag = $element; break; default : $this->activeTag = $element; } } // }}} // {{{ endHandler() /** * End element handler for XML parser * * If the end of <item>, <channel>, <image> or <textinput> * is reached, this function updates the structure array * $this->struct[] and adds the field "type" to this array, * that defines the type of the current field. * * @access private * @param object XML parser object * @param string * @return void */ function endHandler($parser, $element) { if ($element == $this->insideTag) { $this->insideTag = ""; $this->struct[] = array_merge(array("type" => strtolower($element)), $this->last); } if ($element == "ITEM") { $this->items[] = $this->item; $this->item = ""; } if ($element == "IMAGE") { $this->images[] = $this->image; $this->image = ""; } if ($element == "TEXTINPUT") { $this->textinputs = $this->textinput; $this->textinput = ""; } $this->activeTag = ""; } // }}} // {{{ cdataHandler() /** * Handler for character data * * @access private * @param object XML parser object * @param string CDATA * @return void */ function cdataHandler($parser, $cdata) { switch ($this->insideTag) { // Grab general channel information case "CHANNEL" : switch ($this->activeTag) { case "TITLE" : case "LINK" : case "DESCRIPTION" : case "IMAGE" : case "ITEMS" : case "TEXTINPUT" : $this->_add("channel", strtolower($this->activeTag), $cdata); break; } break; // Grab item information case "ITEM" : switch ($this->activeTag) { case "TITLE" : case "LINK" : case "DESCRIPTION" : $this->_add("item", strtolower($this->activeTag), $cdata); break; } break; // Grab image information case "IMAGE" : switch ($this->activeTag) { case "TITLE" : case "URL" : case "LINK" : $this->_add("image", strtolower($this->activeTag), $cdata); break; } break; // Grab image information case "TEXTINPUT" : switch ($this->activeTag) { case "TITLE" : case "DESCRIPTION" : case "NAME" : case "LINK" : $this->_add("textinput", strtolower($this->activeTag), $cdata); break; } break; } } // }}} // {{{ defaultHandler() /** * Default handler for XML parser * * @access private * @param object XML parser object * @param string CDATA * @return void */ function defaultHandler($parser, $cdata) { return; } // }}} // {{{ _add() /** * Add element to internal result sets * * @access private * @param string Name of the result set * @param string Fieldname * @param string Value * @return void * @see cdataHandler */ function _add($type, $field, $value) { $this->{$type}[$field] .= $value; $this->last = $this->{$type}; } // }}} // {{{ getStructure() /** * Get complete structure of RSS file * * @access public * @return array */ function getStructure() { return (array)$this->struct; } // }}} // {{{ getchannelInfo() /** * Get general information about current channel * * This function returns an array containing the information * that has been extracted from the <channel>-tag while parsing * the RSS file. * * @access public * @return array */ function getChannelInfo() { return (array)$this->channel; } // }}} // {{{ getItems() /** * Get items from RSS file * * This function returns an array containing the set of items * that are provided by the RSS file. * * @access public * @return array */ function getItems() { return (array)$this->items; } // }}} // {{{ getImages() /** * Get images from RSS file * * This function returns an array containing the set of images * that are provided by the RSS file. * * @access public * @return array */ function getImages() { return (array)$this->images; } // }}} // {{{ getTextinputs() /** * Get text input fields from RSS file * * @access public * @return array */ function getTextinputs() { return (array)$this->textinputs; } // }}} } ?> Index: pear/XML_RSS/package.xml +++ pear/XML_RSS/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <package> <name>XML_RSS</name> <summary>Parser for Resource Description Framework (RDF) Site Summary (RSS) documents.</summary> <maintainer> <user>MJ</user> <name>Martin Jansen</name> <email>mj@php.net</email> </maintainer> <release> <version>0.9</version> <date>2002-01-11</date> <notes><![CDATA[ ]]> </notes> </release> <filelist> <dir name="/" baseinstalldir="XML"> <file role="php">RSS.php</file> <dir name="tests"> <file role="php">01-rss-parse.phpt</file> <file role="xml">test.rss</file> </dir> </dir> </filelist> </package> Index: pear/XML_RSS/tests/01-parse.phpt +++ pear/XML_RSS/tests/01-parse.phpt --TEST-- XML_RSS::getItems --FILE-- <?php require "../RSS.php"; $r =& new XML_RSS("test.rss"); $r->parse(); foreach ($r->getItems() as $value) { echo $value['title'] . ": " . $value['link'] . "\n"; } ?> --EXPECT-- PHP homepage: http://php.net/ PEAR homepage: http://pear.php.net/ PHP-GTK homepage: http://gtk.php.net/ PHP QA homepage: http://qa.php.net/ Index: pear/XML_RSS/tests/test.rss +++ pear/XML_RSS/tests/test.rss <?xml version="1.0"?> <!-- This is a test file for XML_RSS. --> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://my.netscape.com/rdf/simple/0.9/"> <channel> <title>Test</title> <link>http://pear.php.net/</link> <description>This is a test channel for XML_RSS</description> </channel> <image> <title>PEAR</title> <url>http://pear.php.net/gifs/pearsmall.gif</url> <link>http://pear.php.net/</link> </image> <item> <title>PHP homepage</title> <link>http://php.net/</link> </item> <item> <title>PEAR homepage</title> <link>http://pear.php.net/</link> </item> <item> <title>PHP-GTK homepage</title> <link>http://gtk.php.net/</link> </item> <item> <title>PHP QA homepage</title> <link>http://qa.php.net/</link> </item> <textinput> <title>Search Slashdot</title> <description>Search Slashdot stories</description> <name>query</name> <link>http://slashdot.org/search.pl</link> </textinput> </rdf:RDF>
« previous php.pear.cvs (#1908) next »