PHP 8.1.0 RC 4 available for testing


(PHP 5, PHP 7, PHP 8)



public DOMDocument::save(string $filename, int $options = 0): int|false




The path to the saved XML document.


Additional Options. Currently only LIBXML_NOEMPTYTAG is supported.


Returns the number of bytes written or false if an error occurred.


Beispiel #1 Saving a DOM tree into a file


= new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput true;

$root $doc->appendChild($root);

$title $root->appendChild($title);

$text $title->appendChild($text);



Siehe auch

add a note add a note

User Contributed Notes 6 notes

siegparr at NOSPAM dot web dot de
15 years ago


// Text to insert into XML below

// Create Instance of DOMDocument
$dom =  new DOMDocument;
// Load XML file
// Find the parent node
$parent = $dom->documentElement;
// Create Instance of DomXPath
$xpath = new DomXPath($dom);
// new node will be inserted before this node
// Create the new element
// Insert the new element
$txt = utf8_encode($txt);
// Create new text node with utf-8 encoded string
// Append text node to new element
// save

Hope this helps someone.

chenel324 at gmail dot com
12 years ago
It took me forever to discover that
DOMDocument->formatOutput = true
DOMDocument->preserveWhiteSpace = false ....

Hope this saves somebody a headache.
ludvig at ergopedia dot no
6 years ago




//Add som nodes and text with apropriate methods
dubois-holvoet dot patrick at neuf dot fr
5 years ago
I just intend to bring my little contribution to the use of DOMDocument::save() function along with PHP serialize() function.

Sometimes you could have to serialize a PHP object before you put it into a XML structure (or other..). And then I suppose you could have to save this XML structure in a file. Of course, you will further need to read this file and unserialize it before to use its content.

The problem appears when there are some properties described as Protected in the object to be serialized.

As it’s written in the PHP documentation, serialize function add for those protected properties an asterisk before the name of the property along with a NULL character on each side of the asterisk.
In this case, the DOMDocument::save function will stop the save operation just before the first NULL character met in the string to be saved, because PHP consider it as a potential risk.
So, after that, the unserialize operation of the file become impossible.

To solve that, here is a first solution to apply :

For the serialize , before the DOMDocument::save() ::
$data_serial = explode("\x00\x2A\x00", serialize($object));  //take off the 'NULL * NULL' string
$data_serial = implode("\x5C\x30\x2A\x5C\x30", $data_serial);    //and replace with '\0*\0' string

Before the unserialize :
$data_serial = explode("\x5C\x30\x2A\x5C\x30", $serialized_object);    // take off the '\0*\0' string
$serial_object = implode("\x00\x2A\x00", $data_serial);      // and replace with 'NULL * NULL' string as included by the previous serialize

And a second solution :

$serialized_object = addslashes(serialize($object)) ;

Before re-using the object in your application :
$object = unserialize(stripslashes($serialized_object)) ;

Hope this will help some.
john at johnreid dot it
8 years ago



= 'myxml.xml';
$myfile = realpath($myfile);
$doc = new DOMDocument('1.0');

// Let's just add a couple of elements for good measure
$root = $doc->documentElement;



ss at littlerain dot com
12 years ago
In addition to:
DOMDocument->formatOutput = true
To Top