note 61574 added to function.dom-domdocument-createelement
| From: | inayat_us21 at yahoo dot com | Date: | Tue, 07 Feb 2006 06:05:30 +0000 |
| Subject: | note 61574 added to function.dom-domdocument-createelement | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-103159@lists.php.net to get a copy of this message | ||
Simple way to create an rss feed using php5
<?php
//create xml doc
$doc = new DOMDocument("1.0");
//create the rss element
$root = $doc->createElement('rss');
$root = $doc->appendChild($root);
$root->setAttribute('version','2.0');
//create the channel element
$channel = $doc->createElement("channel");
$channel = $root->appendChild($channel);
$elements = array();
$elements["title"] = "my feed's title";
$elements["link"] = "http://mydomain.com/index.rss";
$elements["description"] = "Demonstration feed created using PHP5 and the DOM
extension. The php script that generated this document can be found at : http://thousandrobots.com/dev/xml/dom/auto.phps";
$elements["language"] = "en-us";
$elements["copyright"] = "copyright 2005 thousandrobots.com";
$elements["docs"] = "http://backend.userland.com/rss";
$elements["generator"] = "thousand robots dom-based rss generator";
$elements["managingEditor"] = "editor@spam.thousandrob0ts.com (ADM)";
$elements["webMaster"] = "webmaster@spam.thousandrob0ts.com (ADM)";
$elements["lastBuildDate"] = "Sat, 05 Feb 2005 23:39:07 EST";
foreach ($elements as $elementname => $elementvalue)
{
$elementname = $doc->createElement($elementname);
$elementname = $channel->appendChild($elementname);
$elementname->appendChild($doc->createTextNode($elementvalue));
}
//clear the array
unset($elements);
//now create the first feed item
//instead, you could just pull some items from a db into an elements[] array.
$elements[0]["title"] = "title of first item";
$elements[0]["description"] = "a short description of the first item";
$elements[0]["link"] = "http://thousandrobots.com/?article=1";
$elements[0]["author"] = "author@spam.thousandrob0ts.com (ADM)";
$elements[0]["pubDate"] = "Sat, 05 Feb 2005 23:39:07 EST";
$elements[0]["category"] = "tech";
$elements[0]["guid"] = "http://thousandrobots.com/?article=1";
//create the other items
$elements[1]["title"] = "title of next item";
$elements[1]["description"] = "a short description of the next item";
$elements[1]["link"] = "http://thousandrobots.com/?article=N";
$elements[1]["author"] = "author@spam.thousandrob0ts.com (ADM)";
$elements[1]["pubDate"] = "Sat, 05 Feb 2005 22:39:07 EST";
$elements[1]["category"] = "tech";
$elements[1]["guid"] = "http://thousandrobots.com/?article=N";
//loop through each item and add its elements to the tree
foreach ( $elements as $element )
{
//create the item element
$item = $doc->createElement("item");
$item = $channel->appendChild($item);
foreach ( $element as $elementname => $elementvalue )
{
$elementname = $doc->createElement($elementname);
$elementname = $item->appendChild($elementname);
$elementname->appendChild($doc->createTextNode($elementvalue));
}
}
//output the xml
header('Content-Type: text/xml');
echo $doc->saveXML();
?>
----
Server IP: 203.199.107.94
Probable Submitter: 196.12.37.123
----
Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-createelement.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+61574
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61574&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61574&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61574&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61574&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61574&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61574&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61574&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+61574&report=yes
Search -- http://master.php.net/manage/user-notes.php