Use PHP5’s DOM support to create a valid RSS 2.0 feed

I was interested in using PHP5’s DOM extension to create RSS feeds, but I couldn’t find a decent tutorial on the web about how to do that. Most of the things out there involve PHP4’s DOM-XML, which is different in some key ways. Even lacks basic examples of how to use even the most fundamental DOM methods.

So I puzzled my way through it, using the sparse examples at and the old DOM-XML samples for creating generic XML docs, trying to find the matching methods in the current DOM reference.

Once you’ve figured out how to create and append one node, the rest is pretty easy. So here is a sample feed generated by PHP and DOM, and here is the heavily commented source code that produces it.

This script demonstrates how to use DOM to create an RSS feed from items that are manually entered into an array. It’s pretty easy to adapt the code to use a database to automatically enter those items into an array: all you’d have to do is query the database, add your items to an array, and substitute that array for the one manually generated in my script. (For the sake of completeness, I will post a script that does this when I get a few minutes.) Update: You can see an example of this fully automatic technique in this Ning app I created.

Leave a Reply

Your email address will not be published. Required fields are marked *