我在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")
?>
我想要标题 - 链接 - 说明 简单的......没有更多
以下是我在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>
格式不错,但有问题。我不明白问题出在哪里。 任何帮助将不胜感激
(我也查看这个网站的任何解决方案,但我找不到我的解决方案..所以,对这篇文章感到抱歉,如果它已经存在的话)
答案 0 :(得分:2)
好吧我发现了我的一种方式..我是通过php用FILES做的:这是代码,如果有人需要帮助:
<?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);
?>