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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
christian_phpbeginner
Forum Contributor
Posts: 136
Joined: Sat Jun 03, 2006 2:43 pm
Location: Java

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

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
Post Reply