DOMDocument::save

(PHP 5)

DOMDocument::save Sauvegarde l'arbre interne XML dans un fichier

Description

public int DOMDocument::save ( string $filename [, int $options ] )

Crée un document XML depuis la représentation DOM. Cette fonction est habituellement appelée après la construction d'un nouveau document DOM, comme dans l'exemple ci-dessous.

Liste de paramètres

filename

Le chemin où sera sauvegardé le document XML.

options

Options additionnelles. Actuellement, seul LIBXML_NOEMPTYTAG est supporté.

Valeurs de retour

Retourne le nombre d'octets écrit ou FALSE si une erreur survient.

Historique

Version Description
5.1.0 Ajout du paramètre options

Exemples

Exemple #1 Sauvegarde d'un arbre DOM dans un fichier

<?php

$doc 
= new DOMDocument('1.0');
// nous voulons un bel affichage
$doc->formatOutput true;

$root $doc->createElement('book');
$root $doc->appendChild($root);

$title $doc->createElement('title');
$title $root->appendChild($title);

$text $doc->createTextNode('This is the title');
$text $title->appendChild($text);

echo 
'Écrit : ' $doc->save("/tmp/test.xml") . ' bytes'// Écrit : 72 bytes

?>

Voir aussi

add a note add a note

User Contributed Notes 4 notes

up
3
siegparr at NOSPAM dot web dot de
7 years ago
The XML parser converts the text of an XML document into UTF-8, even if you have set the character encoding of the XML, for example as a second parameter of the DOMDocument constructor. After parsing the XML with the load() command all its texts have been converted to UTF-8.

In case you append text nodes with special characters (e. g. Umlaut) to your XML document you should therefore use utf8_encode() with your text to convert it into UTF-8 before you append the text to the document. Otherwise you will get an error message like "output conversion failed due to conv error" at the save() command. See example below:

<?php
// Text to insert into XML below
$txt = "a text with special characters like 'ä', 'ß', 'Ü' etc.";

// Create Instance of DOMDocument
$dom =  new DOMDocument;
// Load XML file
// Was created before with DOMDocument('1.0', 'iso-8859-1')
$dom = $dom->load("file.xml");
// Find the parent node
$parent = $dom->documentElement;
// Create Instance of DomXPath
$xpath = new DomXPath($dom);
// new node will be inserted before this node
$next = $xpath->query("//parentnode/childnode");
// Create the new element
$new_elem = $dom->createElement('new_elem');
// Insert the new element
$parent->insertBefore($new_elem, $next->item(0));
// DOMXML = utf-8! (will be converted to iso-8859-1 only at 'save()')
// prevents error message "output conversion failed due to conv error" at 'save()'
$txt = utf8_encode($txt);
// Create new text node with utf-8 encoded string
$nodetext = $dom->createTextNode("$txt");
// Append text node to new element
$nodetext = $new_elem->appendChild($nodetext);
// save
$dom->save("file.xml");
?>

Hope this helps someone.

siegparr
up
3
chenel324 at gmail dot com
5 years ago
It took me forever to discover that
DOMDocument->formatOutput = true
will only have an effect on documents that are loaded from disk if one also sets
DOMDocument->preserveWhiteSpace = false ....

Hope this saves somebody a headache.
up
1
john at johnreid dot it
8 months ago
I had been hitting my head against the wall when I couldn't get an edited XML file to save due to file permissions, even though I knew that it was OK.

Eventually I realised that for some odd reason, when I went to save the XML file, it would try to write the file to the root directory.

As such, use realpath() to keep the complete system path to the XML file when you load it:

<?php
$myfile
= 'myxml.xml';
$myfile = realpath($myfile);
$doc = new DOMDocument('1.0');
$doc->load($myfile);

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

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is a title');
$text = $title->appendChild($text);

$doc->save($myfile);
?>
up
-1
ss at littlerain dot com
5 years ago
In addition to:
DOMDocument->formatOutput = true
DOMDocument->preserveWhiteSpace = false

...you also have to ensure you don't add text nodes as siblings of element nodes, or formatOutput won't work.
To Top