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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/677481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:28:17  来源:igfitidea点击:

Create and Save XML file to Server Using PHP

phpxml

提问by Casey

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。我将如何将 XML 文件保存到服务器上的某个位置,而不是使用 AJAX 来获取 XML?谢谢

回答by Gumbo

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

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

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

回答by Ionu? G. Stan

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

DOMDocument::save()对你没有帮助吗?

回答by JoyGuru

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 文件。可以从这里找到以下代码和教程 - Create and Save XML File using PHP

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

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

回答by Pablo Santa Cruz

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

除了前面两个 ansers 声明的 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 文件的目录中写入的权限。