Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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&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&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&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();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement