如何通过php和MySQL创建xml文件[重复]

时间:2022-09-25 16:04:49

This question already has an answer here:

这个问题在这里已有答案:

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)

我在XML文件中有问题。我通过互联网搜索并找到了很多我的问题的例子,但我不是XML文件的专家,无法解决我的问题。我想做和XML文件一样工作,如RSS FEED。所以,我从我的数据库中获取数据并尝试创建xml代码。这是我在我的php文件中所拥有的(由于这个问题它没有经过验证:未定义的根元素:通道)

<?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:

这是我在rssfeeds.xml文件中得到的:

<?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)

(我也检查这个网站的任何解决方案,但我找不到我的解决方案..所以,对不起这篇文章,如果它已经存在)

1 个解决方案

#1


2  

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

好吧,我发现了我的一种方式..我用FILES通过php做到了:这是代码,如果有人需要帮助:

<?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);
?>

#1


2  

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

好吧,我发现了我的一种方式..我用FILES通过php做到了:这是代码,如果有人需要帮助:

<?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);
?>