2

I have a problem in XML files. I searched through the internet and found lots of examples for my problem but I am not an expert on XML files and couldn't solve my problem. I want to do and XML file and work like RSS FEED. So, I am taking my data from my database and try to create the xml-code. Here what I have in my php file (and it is not validated because of this problem: Undefined root element: channel)

<?php
include "connection.php";
//create the table with the fields
  $rss_table = array(); 
  $query = mysql_query("SELECT * FROM rssfeeds");
    while($values_query = mysql_fetch_assoc($query))
    {
        $rss_table [] = array(
        'title' => $values_query['title'],
        'description' => $values_query['summary'],
        'link' => $values_query['link']
        );
    }

$doc = new DOMDocument(); 
$doc->formatOutput = true;
$doc->encoding = "utf-8";

$r = $doc->createElement( "channel" ); 
$doc->appendChild( $r ); 

   //$i=0;
    foreach( $rss_table as $rss ) 
    { 
    $b = $doc->createElement( "item" );
        $title = $doc->createElement( "title" ); 
        $title->appendChild( 
        $doc->createTextNode( $rss['title'] ) 
        ); 
    $b->appendChild( $title ); 


    $description = $doc->createElement( "description" ); 
    $description->appendChild( 
    $doc->createTextNode( $rss['description'] ) 
    ); 
    $b->appendChild( $description );

    $link = $doc->createElement( "link" ); 
    $link->appendChild( 
    $doc->createTextNode( $rss['link'] ) 
    ); 
    $b->appendChild( $link );

    $r->appendChild( $b );
    }
echo $doc->saveXML();
$doc->save("rssfeeds.xml") 
?>

I want to have title - link - description Simple one... nothing more

And here is what I get in rssfeeds.xml file:

<?xml version="1.0" encoding="utf-8"?>
<channel>
  <item>
    <title>winter week</title>
    <description>You can come as you are! </description>
    <link>http://tdm2000international.org/tdm2000international/news.php</link>
  </item>
  <item>
    <title>Greek night</title>
    <description>elliniki bradua sto magazi</description>
    <link>http://tdm2000international.org/tdm2000international/news.php</link>
  </item>
  <item>
    <title>event website</title>
    <description>first of december, how is it going?</description>
    <link>http://tdm2000international.org/tdm2000international/news.php</link>
  </item>
</channel>

Nice format, but it has problem. I do not understand where the problem is. Any help would be appreciated

(I also check this website for any solution, but I could not found my solution..So, sorry about this post, if it is already exist)

Blackcoat77
  • 1,574
  • 1
  • 21
  • 31
  • What problem does it have? – Louis Huppenbauer Dec 07 '12 at 10:06
  • 1
    How do you know there's a problem? – Osiris Dec 07 '12 at 10:07
  • 1
    What do you mean `and it is not validated because of this problem: Undefined root element: channel`?! Unless you have a schema for your XML document it *won't* be defined. – Rudi Visser Dec 07 '12 at 10:09
  • I have the FeedDemon program and says that this link of RSS is not working.. I put this RSS-link here: http://validator.w3.org/feed/ and it says: Undefined root element: channel What do you mean unless I have a schema for your xml document it won't be defined? – Kiriakos Grhgoriadhs Dec 07 '12 at 20:05

1 Answers1

2

ok I found my one way .. I did it with FILES via php: this is the code if anyone needs help to that:

<?php
include "connection.php";
$myFile = "rss.xml";
$fh = fopen($myFile, 'w') or die("can't open file");

$rss_txt .= '<?xml version="1.0" encoding="utf-8"?>';
$rss_txt .= "<rss version='2.0'>";
$rss_txt .= '<channel>';
    $query = mysql_query("SELECT * FROM rssfeeds");
    while($values_query = mysql_fetch_assoc($query))
    {
        $rss_txt .= '<item>';

        $rss_txt .= '<title>' .$values_query['title']. '</title>';
        $rss_txt .= '<link>' .$values_query['link']. '</link>';
        $rss_txt .= '<description>' .$values_query['summary']. '</description>';

        $rss_txt .= '</item>';
    }
$rss_txt .= '</channel>';
$rss_txt .= '</rss>';

fwrite($fh, $rss_txt);
fclose($fh);
?>