how to create xml files via php and MySQL

14,956

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);
?>
Share:
14,956
Kiriakos Grhgoriadhs
Author by

Kiriakos Grhgoriadhs

I am a Software Engineer, Programmer specializing in Web Development. I have been working as a freelance on web development since 2008-09. Some of the fields that I am specialized are: Development of e-commerce (using platforms such as: WordPress, Joomla, CS-Cart, openCart, but also Building Custom Platform via PHP) Website &amp; Domain Hosting Support Server Administrator's skills Development of information systems according to Customer's needs (Customer Relationship Management) Furthermore, I am developing Windows Mobile Apps since 2011. I have a variety of applications in my portfolio from multiple-players Game apps, to daily common ones. The functionalities of these applications are plenty as well: Use of Geolocation Use of Accelerator Use of Web Services (in order to interact with a Database) use of Sound - Video - Image Libraries Reading and/or Parsing an external XML file and/or JSON data For more information about me, please do not hesitate to contact me!

Updated on July 24, 2022

Comments

  • Kiriakos Grhgoriadhs
    Kiriakos Grhgoriadhs almost 2 years

    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)