4

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

4 Answers 4

11

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

$dom->save('document.xml');
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

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

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

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

Comments

1

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

$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.