Introduction and Quick Start
Вернуться к: XML_Feed_Parser
Introduction and Quick Start
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