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

时间:2021-04-10 09:56:04

I'm using PHP to extract data from a MySQL database. I am able to build an XML file using DOM functions. Then using echo $dom->saveXML(); , I am able to return the XML from an AJAX call. Instead of using AJAX to get the XML, how would I save the XML file to a spot on the server? Thanks

我正在使用PHP从MySQL数据库中提取数据。我能够使用DOM函数构建XML文件。然后使用echo $ dom-> saveXML(); ,我能够从AJAX调用返回XML。而不是使用AJAX来获取XML,我如何将XML文件保存到服务器上的某个位置?谢谢

4 个解决方案

#1


11  

Use the DOMDocument::save() method to save the XML document into a file:

使用DOMDocument :: save()方法将XML文档保存到文件中:

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

#2


3  

Doesn't DOMDocument::save() help you?

DOMDocument :: save()不帮你吗?

#3


2  

Use PHP XML DOM Parser to create and save XML file. The following code and tutorial would be found from here - Create and Save XML File using PHP

使用PHP XML DOM Parser创建和保存XML文件。可以从这里找到以下代码和教程 - 使用PHP创建和保存XML文件

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

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

#4


1  

Besides "save" option of the DOM itself stated by two previous ansers, you could also use this piece of code:

除了以前的两个ansers所说的DOM本身的“save”选项之外,你还可以使用这段代码:

$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文件的目录的权限。

#1


11  

Use the DOMDocument::save() method to save the XML document into a file:

使用DOMDocument :: save()方法将XML文档保存到文件中:

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

#2


3  

Doesn't DOMDocument::save() help you?

DOMDocument :: save()不帮你吗?

#3


2  

Use PHP XML DOM Parser to create and save XML file. The following code and tutorial would be found from here - Create and Save XML File using PHP

使用PHP XML DOM Parser创建和保存XML文件。可以从这里找到以下代码和教程 - 使用PHP创建和保存XML文件

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

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

#4


1  

Besides "save" option of the DOM itself stated by two previous ansers, you could also use this piece of code:

除了以前的两个ansers所说的DOM本身的“save”选项之外,你还可以使用这段代码:

$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文件的目录的权限。