 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
09-26-2007, 03:47 PM
|
#1 (permalink)
|
|
The Reckoner
Join Date: Sep 2007
Posts: 437
Thanks: 22
|
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&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(); ?>
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.
|
|
|
|
09-26-2007, 10:22 PM
|
#2 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
|
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...
|
|
|
|
09-27-2007, 12:07 AM
|
#3 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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();
|
|
|
|
09-27-2007, 01:39 AM
|
#4 (permalink)
|
|
The Acquainted
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
|
Okay, great.
Thanks!
__________________
Not quite a n00b...
|
|
|
|
10-18-2012, 01:21 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Oct 2012
Posts: 244
Thanks: 0
|
Some conservatives have Coach Factory Outlet pushed that critique further, saying that Mr. Obama’s policies are too costly, often assist the wrong people Louis Vuitton Belts and could have the paradoxical effect of driving up college costs. The dispute turns not just on different Coach Factory Outlet assessments of how policies play out, but on differing philosophical views about the role of government. During Gucci Belts his time in office, Mr. Obama has sharply increased aid to low- and middle-income students, notably through the Pell Grant Coach Factory Outlet program, which grew from $14.6 billion given to 6 million students in 2008, to nearly $40 billion for Coach Factory Outlet almost 10 million students this year. His administration also made it easier to request aid, shortening the Coach Factory Online complex federal application and allowing people to transfer their financial information electronically from the Internal Coach Outlet Online Revenue Service database. But while many education experts laud his efforts, analysts of varying political Coach Outlet Online stripes have also questioned how much impact some of the president’s policies will have, noting that the prices Coach Online Outlet charged by colleges, and student borrowing, continue to climb.But behind the headlines about soaring costs, the Coach Factory Outlet Online reality is more complex and wildly uneven, because a growing number of students receive Coach Outlet Online financial aid, and only relatively high-income families pay those fast-rising sticker prices. Adjusted for Coach Factory Online inflation, the College Board calculates, the average net price changed little over the last decade at private Coach Factory Outlet schools, and rose only modestly at public ones.Defending federal spending, Arne Duncan, the secretary of Hermes Belts education, said that for more than 30 years, college prices had risen even when federal aid had not, leading him to believe Coach Factory Online there was zero correlation.
|
|
|
|
10-22-2012, 09:39 AM
|
#7 (permalink)
|
|
The Addict
Join Date: Oct 2012
Posts: 244
Thanks: 0
|
Coach Outlet
You’ve relatively Coach Outlet recently arrived in New Delhi after living in two of Asia’s other great cities, Coach Outlet Store Online Tokyo and Hong Kong, for several years. Do these cities feel like they’re part of the same continent? Yes, and no. In terms Coach Factory Onlineof infrastructure, they couldn’t be more different. Getting regular Coach Outlet power and water at my house in New Delhi is never a sure thing, even though Coach Purse Outlet OnlineI’m paying the same rent that I paid in Tokyo and almost the same electricity prices. Both Hong Kong and Tokyo are also crowded places, Coach Factory Outlet Online but both cities are incredibly well planned and efficiently run. Efficient is not a word I would use to describe my Coach Bags Outlet Onlineday-to-day life in New Delhi. On the other hand, one thing that I think Hong Kong and New Delhi have in common is Coach Handbags Outlet a shared sense of optimism — a feeling that the best is yet to come. That’s definitely not the feeling you get in Tokyo, Coach Outlet Online or in the U.S. when I go home. It’s a big part of what I find addictive about living and working in this part of the world. You feel like you’re watching the future unfold.
|
|
|
|
01-29-2013, 11:58 AM
|
#8 (permalink)
|
|
The Addict
Join Date: Oct 2012
Posts: 244
Thanks: 0
|
Organizers said Coach Outlet Online was opportune because the battle’s 150-year anniversary is in December, and Fredericksburg Coach Factory Outlet has been preparing to mark the sesquicentennial. in the new agreement is that Coach Outlet Online revolutionary councils from 14 Syrian provinces now each have a representative, though not all live Coach Online Outlet in Syria. The hope is that will bind the coalition to those inside the country. Perhaps Coach Bags Outlet the most important body the new group is expected to form is a Revolutionary Military Council Coach Factory Online to oversee the splintered fighting organizations and to funnel both lethal and nonlethal Coach Factory Outlet military aid to the rebels. It should unite units of the Free Syrian Army, various militias Coach Outlet Store Online and brigades in each city and large groups of defectors. Before the ink was even dry on the Coach Outlet Store final draft, negotiators hoped that it would bring them the antiaircraft missiles they crave to Coach Factory Stores take on the Syrian Air Force. The United States and Britain have offered only Coach Handbags Outlet nonmilitary aid to the uprising. A similar attempt by the Syrian National Council to Coach Factory Store supervise the military never jelled. Organizers said funding was too haphazard. Eventually foreign Coach Factory Online governments like Qatar and Saudi Arabia, which are financing and arming the rebels, found Coach Factory Online their own favorite factions to deal with. Foreign leaders notably including Secretary of State Coach Outlet Hillary Rodham Clinton urged this unification largely so they could coordinate their Coach Factory Outlet efforts and aid through a group of technocrats. Once it receives international recognition, the Coach Outlet Store Online coalition is supposed to establish a temporary Coach Outlet Online military never jelled.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|