简体   繁体   English

使用 PHP 创建 XML 文件并将其保存到服务器

[英]Create and Save XML file to Server Using PHP

I'm using PHP to extract data from a MySQL database.我正在使用 PHP 从 MySQL 数据库中提取数据。 I am able to build an XML file using DOM functions.我能够使用 DOM 函数构建 XML 文件。 Then using echo $dom->saveXML();然后使用echo $dom->saveXML(); , I am able to return the XML from an AJAX call. ,我能够从 AJAX 调用返回 XML。 Instead of using AJAX to get the XML, how would I save the XML file to a spot on the server?我如何将 XML 文件保存到服务器上的某个位置,而不是使用 AJAX 来获取 XML? Thanks谢谢

Use the DOMDocument::save() method to save the XML document into a file:使用DOMDocument::save()方法将 XML 文档保存到文件中:

$dom->save('document.xml');

Doesn't DOMDocument::save() help you? DOMDocument::save()对您没有帮助吗?

Use PHP XML DOM Parser to create and save XML file.使用 PHP XML DOM Parser 创建并保存 XML 文件。 The following code and tutorial would be found from here - Create and Save XML File using PHP可以从这里找到以下代码和教程 -使用 PHP 创建和保存 XML 文件

$xmlString = 'Insert XML Content';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);

$dom->save('fileName.xml');

Besides "save" option of the DOM itself stated by two previous ansers, you could also use this piece of code:除了前面两个分析器声明的 DOM 本身的“保存”选项之外,您还可以使用这段代码:

$strxml = $dom->saveXML();
$handle = fopen("yourxmlfile.xml", "w");
fwrite($handle, $strxml);
fclose($handle);

And you are done.你完成了。

Remember that the user running your application server (Apache, probably) will need permissions to write in the directory you are placing the XML file.请记住,运行您的应用程序服务器(可能是 Apache)的用户需要在您放置 XML 文件的目录中写入权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM