Page 1 of 1

When to use DOMDocument->save() OR saveXML() ?

Posted: Fri Dec 15, 2006 12:05 am
by christian_phpbeginner
Hi folks,

I am using DOMDocument to create a xml file...but I don't quiet understand about the difference between the DOMDocument->save() and DOMDocument->saveXML()....

From what I have read in the documentation about these I only noted the difference that the saveXML() method doesn't output to the file, while the save() method does output a file..., please correct me if I am wrong...

http://www.php.net/manual/en/function.d ... t-save.php
http://www.php.net/manual/en/function.d ... avexml.php

So, which one is better to use ??? Or should I use both ?

Many thanks,
Christian

Posted: Fri Dec 15, 2006 12:27 am
by Kieran Huggins
I suspect saveXML() is what you're looking for.

The save() method writes a file to the filesystem

Code: Select all

$doc = new DOMDocument;

//.... add some nodes....

$doc-->save('path/to/file.xml'); // saves the xml as a file on the server

// maybe you want the user to download the file?
echo '<a href="path/to/file.xml">download the xml file</a>';
The saveXML() method echoes the XML to the output (like the browser, or you can capture it as a variable)

Code: Select all

$doc = new DOMDocument;

//.... add some nodes....

$xml_as_a_string = $doc-->saveXML(); // saves the xml in the variable
 // --OR--
$doc-->saveXML(); // writes the xml to the browser
you might want to save the xml to a variable if you plan on transforming it.

Cheers,
Kieran