TMichel
TMichel

Reputation: 4452

PHP string to XML file

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

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

Appreciate any help.

Upvotes: 10

Views: 25645

Answers (4)

Max
Max

Reputation: 1040

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

But if you also want to parse the XML, use SimpleXML (or even better: 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.

Upvotes: 0

Teneff
Teneff

Reputation: 32148

you can use:

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

Upvotes: 25

boobiq
boobiq

Reputation: 3024

you can use file_put_contents for example

Upvotes: 0

Jeff
Jeff

Reputation: 380

You can use DOM to load the XML string 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

Upvotes: 2

Related Questions