Creating RSS documents with the DOM API

Posted by admin on December-11-2009 Add Comments

Bookmark and Share


This article illustrates how you can use the PHP5 DOM API to create your own RSS files. When used in conjunction with RSS Parsing with SimpleXML, these articles will help you create and read RSS files by utilizing two of the new PHP5 extensions, SimpleXML and DOM.

If you haven’t read my article on reading RSS files with SimpleXML, you can do so here.

Like my previous article, the code is heavily commented to help explain what it does.

PHP Code:
<?php

// Create a new DOM Document object, this will create a new XML declaration for us.
// We can enter the version number and encoding (in that order), by passing
// two arguments to the object's constructor.  By default, the XML
// declaration's version attribute will be set to "1.0"
$pDom = new DOMDocument();

// Here we create a new root elelement named rss.
$pRSS = $pDom->createElement('rss');

// We now add a new attribute to the rss element.  We name this new
// attribute version and give it a value of 0.91.
$pRSS->setAttribute('version', 0.91);

// Finally we append the attribute to the XML tree using appendChild
$pDom->appendChild($pRSS);

// We repeat the same process again here, but this time we're creating
// the channel element.
$pChannel = $pDom->createElement('channel');

$pRSS->appendChild($pChannel);

// Create the main child nodes of channel, these contain the information
// related to this RSS file.  I'm not going to comment each one of these
// as they should be easy enough to understand.  Basically we're creating
// a new element for each node, the first argument specifies the name of
// the element we're creating, and the second specifies the text value
// of the node, for example, title would render as:  <title>TalkPHP</title>
$pTitle = $pDom->createElement('title', 'TalkPHP');
$pLink = $pDom->createElement('link', 'http://www.talkphp.com');
$pDesc = $pDom->createElement('description', 'Discuss PHP and other various web related topics in a knowledgeable and friendly community.');
$pLang = $pDom->createElement('language', 'en');
$pImage = $pDom->createElement('image');

// Here we simply append all the nodes we just created to the channel node
$pChannel->appendChild($pTitle);
$pChannel->appendChild($pLink);
$pChannel->appendChild($pDesc);
$pChannel->appendChild($pLang);
$pChannel->appendChild($pImage);

// Create three new elements that are needed to "describe" our image
$pURL = $pDom->createElement('url', 'http://www.talkphp.com/images/misc/rss.jpg');
$pTitle = $pDom->createElement('title', 'TalkPHP');
$pLink = $pDom->createElement('link', 'http://www.talkphp.com');

// Append these new elements to the image element
$pImage->appendChild($pURL);
$pImage->appendChild($pTitle);
$pImage->appendChild($pLink);

// This array represents the data contained within the imaginary database.
$aLatestThreads = array
(
array
(
'title' => "G'day",
'link' => 'http://www.talkphp.com/showthread.php?t=1107&amp;goto=newpost',
'description' => "Forum: Member Introductions
Posted By: Shaun
Post Time: 09-14-2007 at 01:15 PM"
),

array
(
'title' => "AJAX File Uploader",
'link' => 'http://www.talkphp.com/showthread.php?t=1106&amp;goto=newpost',
'description' => "Forum: Javascript, AJAX, E4X
Posted By: CreativeLogic
Post Time: 09-13-2007 at 07:32 PM"
,
),

array
(
'title' => "Some sites of mine...",
'link' => 'http://www.talkphp.com/showthread.php?t=1104&amp;goto=newpost',
'description' => "Forum: Show Off
Posted By: Craddock
Post Time: 09-13-2007 at 04:54 PM"
)

);

// Loop trough each result from our imaginary database, these are the
// RSS items that the viewer of the RSS will see
foreach ($aLatestThreads as $aThread)
{
// Nothing new here, we're creating an item element as our parent
// and then creating and adding three child nodes to it.
$pItem = $pDom->createElement('item');
$pTitle = $pDom->createElement('title', $aThread['title']);
$pLink = $pDom->createElement('link', $aThread['link']);
$pDesc = $pDom->createElement('description', $aThread['description']);

// Append the nodes to the item, then append the item to the channel

$pItem->appendChild($pTitle);
$pItem->appendChild($pLink);
$pItem->appendChild($pDesc);

$pChannel->appendChild($pItem);
}

// Set content type to XML, thus forcing the browser to render is as XML
header('Content-type: text/xml');

// Here we simply dump the XML tree to a string and output it to the browser
// We could use one of the other save methods to save the tree as a HTML string
// XML file or HTML file.
echo $pDom->saveXML();
?>

You can see the output from this script here.

In conclusion, the DOM extension gives you the power you need to manipulate the DOM. Used in conjunction with SimpleXML, you have all the functionality you should need, these two extensions allow you to to easily read, write and modify XML documents using PHP 5.

Post a Comment

You must be logged in to post a comment.