Article
PHP and XML: Parsing RSS 1.0
Summary and Resources for Further Reading
In this article we learned how to parse XML documents (taking the specific example of a RSS 1.0 document) using PHP. Although its event-based API for XML document processing can require some thought to determine how to process even a simple XML document structure, we saw that thinking of the events as they occur in the processing of the document can help to determine what each of the event handling methods need to do.
In a fully-coded example, I demonstrated how to parse SitePoint's RSS file, which lists the cover articles on SitePoint.com at any given time. Feel free to adapt this code for use on your site to list the current headlines on SitePoint.com in whatever format suits your site! For the programming purists out there, I provided an alternate version of the code that avoids the use of global variables by encapsulating the event handlers and the variables they must share inside a PHP class. With a little more work, you could encapsulate all of the XML processing code within the class and place that class in an include file, thus reducing the actual code in your document for displaying the SitePoint headlines to a two-line affair of creating your RSSParser object and then passing one of its methods the URL of SitePoint's RSS file (http://www.sitepoint.com/rss.php).
For more information on the RSS 1.0 specification, visit the RSS Working Group's Web site. This site also includes links to information about the older RSS 0.9x standards that are still in use by many sites on the Web today, as RSS 1.0 is still fairly new. You'll be happy to know that the RSS 0.9x formats are just as easy to parse with PHP as RSS 1.0, if not moreso.
To see the code involved in parsing RSS 0.91 documents, see Mark Robards' article, Parsing XML With PHP. This short but informative article demonstrates a technique for parsing XML data with PHP that minimizes the number of global variables (or instance variables, if you use an Object Oriented approach) required. Personally I prefer readable code to clever tricks like these, but this technique could considerably simplify the parsing of very complex XML documents.