Hello,

I am trying to create an rss feed using php dom. I have created regular xml files before... but I am running into a problem with proper nesting for the rss feed.

here is what I have so far:

<?php
//create doctype
$dom = new DOMDocument("1.0");

//display document in browser as plain text
//for readability purposes
header("Content-Type: text/plain");

//create root element
$root = $dom->createElement("rss");
$dom->appendChild($root);

//create attribute for pep
$version = $dom->createAttribute("version");
$root->appendChild($version);

//create attribute value for pep
$versionValue = $dom->createTextNode("2.0");
$version->appendChild($versionValue);

//create channel with in rss
$channel = $dom->createElement("channel");
$root->appendChild($channel);

//create channel with in rss
$title1 = $dom->createElement("title");
$root->appendChild($title1);

//create text node
$textTitle = $dom->createTextNode("Sono-Tek Corporation News");
$title1->appendChild($textTitle);

//create channel with in rss
$link1 = $dom->createElement("link");
$root->appendChild($link1);


//save and display tree
echo $dom->saveXML();

?>

if anyone knows of any tutorials that use DOM to create RSS feeds let me know. The ones I have found so far are just for regular xml...

Thanks for the help.

    And how is RSS not "regular XML"?

    Besides, couldn't you just write it yourself? It's not that different from HTML.

    <?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">
      <channel>
        <link>http://www.example.com/</link>
        <language>en-gb</language>
        <description>Whatever you Find These Days at Example.com</description>
        <title>Example.com RSS Feed</title>
        <item>
          <description>Example Comic Strip</description>
          <title>Daily Example</title>
          <guid isPermaLink="false">Daily Example</guid>
        </item>
    ....
    </channel>
      </rss>
    

      Well I meant a basic XML file. But i figured out what I wanted to do last night

      here is the code:

      <?php
      //connect to the databse and create a query
      mysql_connect("","","")or die(mysql_error()); 
      mysql_select_db("")or die(mysql_error()); 
      
      $query = "";
      
      $pullDate = mysql_query($query);
      $static = mysql_fetch_array($pullDate);
      
      //create doctype
      $dom = new DOMDocument("1.0");
      
      //create rss root with values
      $rss = $dom->createElement("rss");
      $dom->appendChild($rss);
      $version = $dom->createAttribute("version");
      $rss->appendChild($version);
      $versionValue = $dom->createTextNode("2.0");
      $version->appendChild($versionValue);
      
      //create channel element
      $channel = $dom->createElement("channel");
      $rss->appendChild($channel);
      
      /********** Create Static Section *******************/
      
      //create static information
      $Stitle =  $dom->createElement("title");
      $Slink = $dom->createElement("link");
      $Sdesc = $dom->createElement("description");
      $language = $dom->createElement("language");
      $SpubDate = $dom->createElement("pubdate");
      $Sauthor = $dom->createElement("author");
      $webmaster = $dom->createElement("webmaster");
      
      //create text nodes for static info
      $StitleText = $dom->createTextNode("Sono-Tek Corporation News");
      $SlinkText = $dom->createTextNode("http://www.sono-tek.com/");
      $SdescText = $dom->createTextNode("Visit sono-tek.com to get the latest news and information on product releases.");
      $languageText = $dom->createTextNode("en-us");
      $SpubDateText = $dom->createTextNode($static['pubdate']);
      $SauthorText = $dom->createTextNode("info@sono-tek.com");
      $webmasterText = $dom->createTextNode("websupport@sono-tek.com");
      
      //add text to static elements
      $Stitle->appendChild($StitleText);
      $Slink->appendChild($SlinkText);
      $Sdesc->appendChild($SdescText);
      $language->appendChild($languageText);
      $SpubDate->appendChild($SpubDateText);
      $Sauthor->appendChild($SauthorText);
      $webmaster->appendChild($webmasterText);
      
      //append static items to channel
      $channel->appendChild($Stitle);
      $channel->appendChild($Slink);
      $channel->appendChild($Sdesc);
      $channel->appendChild($language);
      $channel->appendChild($SpubDate);
      $channel->appendChild($Sauthor);
      $channel->appendChild($webmaster);
      
      /************* End Static Information **********/
      
      /********** Dynamic section ***************/
      
      $run = mysql_query($query);
      
      while($items = mysql_fetch_array($run)) {
      	//create elements
      	$item = $dom->createElement("item");
      	$title = $dom->createElement("title");
      	$link = $dom->createElement("link");
      	$desc = $dom->createElement("description");
      	$author = $dom->createElement("author");
      	$pubdate = $dom->createElement("pubdate");
      
      //create the text nodes for item
      $titleText = $dom->createTextNode($items['title']);
      $linkText = $dom->createTextNode($items['link']);
      $descText = $dom->createTextNode($items['description']);
      $authorText = $dom->createTextNode($items['author']);
      $pubDateText = $dom->createTextNode($items['pubdate']);
      
      //add text to the elements
      $title->appendChild($titleText);
      $link->appendChild($linkText);
      $desc->appendChild($descText);
      $author->appendChild($authorText);
      $pubdate->appendChild($pubDateText);
      
      //add elements to the item element
      $item->appendChild($title);
      $item->appendChild($link);
      $item->appendChild($desc);
      $item->appendChild($author);
      $item->appendChild($pubdate);
      
      //append the <item> element to the <channel> element
      $channel->appendChild($item);
      }
      
      #append the <channel> element to the root element <rss>
      $dom->documentElement->appendChild($channel);
      
      //save and display tree
      $dom->save("XML/order.xml");
      
      ?>
      
        Write a Reply...