TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (2) Thread Tools Search this Thread Display Modes
Old 09-26-2007, 04:47 PM   2 links from elsewhere to this Post. Click to view. #1 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 438
Thanks: 22
Karl is on a distinguished road
Default Creating RSS documents with the DOM API

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.
Karl is offline  
Reply With Quote
Old 09-26-2007, 11:22 PM   #2 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Hey,

Thanks for this, just a quick question regarding this... I once wrote this code:

PHP Code:
<?php
// Header
header('Content-type: text/xml');
header('Pragma: public');        
header('Cache-control: private');
header('Expires: -1');

// GET
switch($_GET['xml']) {
// Roster
case "roster" :

    switch(
$_GET['brand']) {
    
// RAW
    
case "raw" :

        
// Querys
        
$raw mysql_query("SELECT * FROM diary_roster WHERE status = '".$_GET['brand']."'") or die(mysql_error());
        
        
// XML
        
echo('<?xml version="1.0" encoding="utf-8"?>
        <diary>
          <raw>'
);
          while(
$row mysql_fetch_array($raw)) {
          echo(
'
            <superstar>
              <id>'
.$row['id'].'</id>
              <name>'
.$row['name'].'</name>
              <status>'
.$row['status'].'</status>
              <alignment>'
.$row['alignment'].'</alignment>
            </superstar>
          '
);
          }
          echo(
'</raw>
        </diary>'
);
    
    break;
    }

break;
}
?>
Basically I can do all that in a more readable format using DOM?
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 09-27-2007, 01:07 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 753
Thanks: 2
Salathe is on a distinguished road
Default

I don't know about being more readable but you could replace the bunch of code under the // XML comment with this DOM code (just an example).

PHP Code:
// Create our document
$pDom  = new DOMDocument('1.0''utf-8');
$pRoot $pDom->appendChild($pDom->createElement('diary'));
$pRaw  $pRoot->appendChild($pDom->createElement('raw'));

// Create <superstar> nodesets
while ($row mysql_fetch_array($raw))
{
    
$pStar $pRaw->appendChild($pDom->createElement('superstar'));
    
$pStar->appendChild($pDom->createElement('id'$row['id']));
    
$pStar->appendChild($pDom->createElement('name'$row['name']));
    
$pStar->appendChild($pDom->createElement('status'$row['status']));
    
$pStar->appendChild($pDom->createElement('alignment'$row['alignment']));
}

// Output formatted XML
header('Content-type: text/xml');
$pDom->formatOutput true;
echo 
$pDom->saveXML(); 
__________________
Salathe is offline  
Reply With Quote
Old 09-27-2007, 02:39 AM   #4 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Okay, great.

Thanks!
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 12-02-2007, 03:16 PM   #5 (permalink)
The Visitor
Newcomer 
 
Join Date: Dec 2007
Posts: 1
Thanks: 0
soft4you is on a distinguished road
Default

good and completely free php script (RSS->HTML)
http://www.extralabs.net/feed2html.htm
soft4you is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/general/1234-creating-rss-documents-dom-api.html
Posted By For Type Date
Quick Web Source - creating rss documents with the dom api This thread Refback 12-29-2007 09:51 AM
Online PHP Tutorials This thread Refback 12-28-2007 01:21 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:32 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0