我目前有一个用PHP编写的脚本,我连接到phpMyAdmin
中的数据库,然后将所有表值解析为一个XML文档。在PHP中创建/写入XML文件?
这里是脚本如何工作的:
$xmlBody .= "<XML>";
$sql_news = mysql_query("SELECT * FROM news_table");
$xmlBody .= "<News_Table>";
//this while loop churns out the values of our "news_table" table
while($row_news = mysql_fetch_array($sql_news)){
// Set DB variables into local variables for easier use
$id_news = $row_news["news_id"];
$author_news = $row_news["news_author"];
$time_news = $row_news["news_time"];
$title_news = $row_news["news_title"];
$content_news = $row_news["news_content"];
$desc_news = $row_news["news_description"];
$image_news = $row_news["news_image"];
$xmlBody .= '
<News_Table_Entry>
<DataID>' . $id_news . '</DataID>
<DataAuthor>' . $author_news . '</DataAuthor>
<DataTime>' . $time_news . '</DataTime>
<DataTitle>' . $title_news . '</DataTitle>
<DataContent>' . $content_news . '</DataContent>
<DataDesc>' . $desc_news . '</DataDesc>
<DataImage>' . $image_news . '</DataImage>
</News_Table_Entry>';
} // End while loop
$xmlBody .= "</News_Table>";
mysql_close(); // close the mysql database connection
$xmlBody .= "</XML>";
echo $xmlBody;
?>
如何创建和输出此作为外部XML file
?我已经成功地获得了此脚本的工作,但是使用所有写入XML的方法无法正常工作。使用
echo 'Wrote: ' . $doc->save("/tmp/test.xml") . ' bytes'; // Wrote: 72 bytes
函数不工作,以及fwrite函数以及。我一直在试图弄清楚这几天,但没有一个我被告知尝试的解决方案已经奏效。任何帮助或见解将不胜感激!
对于可伸缩性和维护,您应该查看序列化。看看http://pear.php.net/package/XML_Serializer/redirected – 2012-03-07 06:11:43
'$ doc'?你没有使用'DOMDocument'或'SimpleXML',对吧?因此,没有定义'save'方法。 – Raptor 2012-03-07 06:14:23
你得到的解决方案? – 2012-03-07 06:27:50