在PHP中创建/写入XML文件? [英] Creating/Writing an XML file in PHP?

查看:152
本文介绍了在PHP中创建/写入XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个用PHP编写的脚本,其中我连接到 phpMyAdmin 中的数据库,然后将所有表值解析为XML文档。

I currently have a script written in PHP where I connect to a database in phpMyAdmin, and then parse all of the table values into an XML document.

脚本的工作原理如下:

    $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文件?我已经成功地让这个脚本工作,但使用所有的方法写出到XML是不工作。使用

How do I create and output this as an external XML file? I've successfully got this script working, but using all of the methods for writing out to XML isn't working. Using the

echo 'Wrote: ' . $doc->save("/tmp/test.xml") . ' bytes'; // Wrote: 72 bytes

函数不工作,还有fwrite函数。我一直在努力想出这几天,但没有一个解决方案,我被告知试试已经工作。

Function isn't working, along with the fwrite function as well. I've been working at trying to figure this out for a few days, but none of the solutions I've been told to try out have worked. Any help or insight would be greatly appreciated!

推荐答案

根据您的代码,您已经自己构建XML内容了。 XML文件只是常规的文本文件,因此在这种情况下,您不需要任何验证和渲染的特殊XML函数。相反,您只需将文本保存到.xml文件:

According to your code, you're already building the XML content yourself. XML files are just regular text files, so in this case you don't need any of the special XML functions that validate and render. Instead, you can simply save your text to the .xml file:

file_put_contents('/tmp/test.xml', $xmlBody);

file_put_contents 允许您放弃所有fopen / fwrite函数,因此它是将内容写入磁盘的最简单方法。

file_put_contents allows you to forego all the fopen/fwrite functions, so it's the easiest way to write content to disk.

另一方面,如果你想学习构建一个结构化的XML文档,并具有所有的一致性的钟声和口哨,请查找 SimpleXML XMLWriter 。这种方式有点额外的开销,但用手工做所有的标记可能是不方便的,特别是当一个错字会使您的整个文档无效。

On the other hand, if you do want to learn to build a structured XML document with all the bells and whistles of consistency, look up SimpleXML or XMLWriter. A little more overhead that way, but doing all the markup by hand can be unwieldy, especially when one typo can invalidate your whole document.

这篇关于在PHP中创建/写入XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆