About
Loads and parses RDF/RSS files. These are XML files that are used by many websites to syndicate headlines. The class provides an interface to the loaded file and makes it simple to generate lists/tables of the source site's headlines.
From version 2.0.0 onwards the class uses PHP's built in XML parser. This has greatly increased the speed of the parser, but limits its use to PHP 4 and above.
From version 2.1.0 onwards basic support has been added for Atom files. These are more complex than RDF/RSS files but for the purposes of this parser are treated identically, the same fields can be retrieved from the RDFParser class as for RDF/RSS files. No special action is required to use Atom files, simply pass the filename to the parser as normal.
Installation
All the code is contained in the parse_rdf.php file; simply copy that in to a directory that your webserver can access and you're good to go. See below for the API documentation and an example script.
API Documentation
A single RDF/RSS file is represented by the RDFParser class. Below are the methods provided by the class.
An RDF/RSS file contains three types of entities: channels, items and fields. The file will contain one channel and any number of items. Both the channel and items will contain fields; the fields are key/value pairs that contain string data.
To use the Slashdot RDF feed as an example. The channel contains information about the site itself; contained in fields such as "title", "description", and "link". The items each contain information about a single story; again contained in fields such as "description" and "link".
- RDFFile ( filename )
-
filename (string ) The filename of the RDF/RSS XML file to be parsed.
- getFieldnames ( )
- Returns an array containing the names of all the channel's fields.
- getField ( field )
-
field (string ) The name of the channel field.
- getFields ( fields )
-
fields (string ) An array of channel field names.
- getNumberOfItems ( )
- Returns the number of items in the file.
- getItemFieldnames ( index )
-
index (integer ) The index of the item.
- getItemField ( index, field )
-
index (integer ) The index of the item. field (string ) The name of the item field.
- getItemFields ( index, fields )
-
index (integer ) The index of the item. fields (string ) An array of item field names.
- getFileModifiedTime ( )
- Returns the last modified time of the XML file used to create this object. The date/time is returned as the number of seconds since the epoch (i.e. Unix time).
Example Usage
Below is an example of how the RDFFile class is used. The code loads the "headline.xml" file (some RDF/RSS file) and displays the title, link and description of the channel. Then it iterates through the channel's items and displays their titles, links and descriptions.
include_once ( 'parse_rdf.php' );
// Output basic HTML header
print "<html><body>\n";
// Load the file in to an RDFFile instance
$file = new RDFFile ( 'headlines.xml' );
// Output the channel information, for clarity the data is copied in to three local variables
list ( $title, $link, $description ) = $file->getFields ( array ( 'title', 'link', 'description' ) );
print "<h1><a href=\"$link\">$title</a></h1>\n";
print "<p>$description</p>\n";
// Output the channel's items, again the items' data is copied in to local variables for clarity
print "<dl>\n";
for ( $index = 0, $noitems = $file->getNumberOfItems ( ); $index < $noitems; $index++ )
{
list ( $title, $link, $description ) = $file->getItemFields ( $index, array ( 'title', 'link', 'description' ) );
print "<dh><a href=\"$link\">$title</a></dh>\n";
print "<dd>$description</dd>\n";
}
print "</dl>\n";
// Output basic HTML footer
print "</html></body>";
?>