PHP 字符串到 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9077181/
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-26 06:07:03  来源:igfitidea点击:

PHP string to XML file

phpxml

提问by TMichel

In PHP, i have a $stringthat contains XML-structured data.

在 PHP 中,我有一个$string包含 XML 结构化数据的数据。

How can i create and save a XML file from $string?

如何创建和保存 XML 文件$string

Appreciate any help.

感谢任何帮助。

回答by Teneff

you can use:

您可以使用:

file_put_contents("myxmlfile.xml", $string);

回答by Jeff

You can use DOM to load the XML string http://www.php.net/manual/en/domdocument.loadxml.php

您可以使用 DOM 加载 XML 字符串 http://www.php.net/manual/en/domdocument.loadxml.php

and then use DOM to save the XML to file http://www.php.net/manual/en/domdocument.save.php

然后使用 DOM 将 XML 保存到文件 http://www.php.net/manual/en/domdocument.save.php

回答by boobiq

you can use file_put_contentsfor example

例如,您可以使用file_put_contents

回答by Maximilian Zellhofer

For just storing the XML to a file, go with Teneff's answer.

对于仅将 XML 存储到文件,请使用 Teneff 的答案。

But if you also want to parse the XML, use SimpleXML(or even better: XMLReader/ XMLWriter).

但是,如果您还想解析 XML,请使用SimpleXML(甚至更好:XMLReader/ XMLWriter)。

Of course, you could also use DOM, but out of all the possibilities around, this would be the slowest and most memory consuming one.

当然,您也可以使用DOM,但在所有可能的情况下,这将是最慢和最消耗内存的一种。