PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » XML » XML_Feed_Parser » Manual

XML_Feed_Parser is a parser for (the various) RSS and Atom format XML feeds. It provides a somewhat unified API while still allowing access to the full details of each feed type.

Introduction and Quick Start

Introduction and Quick Start – Overview of XML_Feed_Parser

Description

XML_Feed_Parser provides a generic interface to a number of the most popular XML-based syndication formats (Atom, RSS1, RSS2, etc). In order to focus on its core competencies, it does not provide any HTTP or feed creation features, but instead parses a wide range of formats quickly and simply.

Presuming the XML for a feed is stored in the variable $xml_source, the simplest way to create an instance of the parser is:

<?php
try {
    $feed = new XML_Feed_Parser($xml_source);
} catch (XML_Feed_Parser_Exception $e) {
    die('Feed invalid: ' . $e->getMessage());
}
?>

The constructor accepts a number of parameters, as follows:

<?php
/* The file you wish to parse */
$source = 'my_source.xml';

/* Where Relax NG is available, we can force validation of the feed */
$validate = true;

/* Whether or not to suppress non-fatal warnings */
$suppress_warnings = false;

/* If the feed is not valid XML and the tidy extension is installed we can
 * attempt to use it to fix the feed */
$use_tidy = true;

$feed = new XML_Feed_Parser($source, $validate, $suppress_warnings, $use_tidy);
?>

Once you have an instance of the parser you can extract feed-level data by querying it directly. eg.

<?php
$title = $feed->title;
?>

You can also access elements by iterating over the feed, or directly by offset or id.

<?php
foreach ($feed as $entry) {
    print $entry->title . "\n";
}

$first_entry = $feed->getEntryByOffset(0);

$particular_entry = $feed->getEntryById('http://jystewart.net/entry/1');
?>

XML_Feed_Parser and Extensions

XML_Feed_Parser and Extensions – Handling extensions with XML_Feed_Parser

Description

Using XML namespaces, the various syndication formats are very easy to extend and the number of extensions in use is enormous.

XML_Feed_Parser focuses on core functionality shared between the various syndication formats. Some of the most common extensions are handled natively—particularly when they provide one format with features to bring it into line with others, such as the content extension for RSS2—but the majority provide specialist content and providing support for them would quickly result in a bloated package.

It is possible that a proper extensions mechanism may be introduced in a future version, but as an alternative the DOM models in use within the classes are publicly accessible, allowing the package to be wrapped with special handlers.

For example, to make use of the 'pheed' extension (namespace http://www.pheed.com/pheed/) we might use:

<?php
$feed = new XML_Feed_Parser($xml_source);
$entry = $feed->getEntryByOffset(0);
$eModel = $entry->model;

$thumbnails = $eModel->getElementsByTagNameNS(
    'http://www.pheed.com/pheed/', 'thumbnail');

if ($thumbnails->length) {
    $thumbnail_url = $thumbnails->item(0)->nodeValue;
}
?>