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:
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:
/* 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.
$title = $feed->title; |
You can also access elements by iterating over the feed, or directly by offset or id.
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 | Уровень выше | XML_Feed_Parser and Extensions |