PHP: DOMDocument->save() - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument->saveHTML() DOMDocument->relaxNGValidateSource()

Last updated: Sat, 27 Jan 2007

view this page in

# DOMDocument->save()

(no version information, might be only in CVS)

DOMDocument->save() -- Dumps the internal XML tree back into a file

## Описание

class DOMDocument {
mixed save ( string filename [, integer options] )
}

Creates an XML document from the DOM representation. This function is usually called after building a new dom document from scratch as in the example below.

## Список параметров

`filename`

  The path to the saved XML document.

`options`

  Additional Options. Currently only LIBXML_NOEMPTYTAG is supported.

## Возвращаемые значения

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

## Список изменений

Версия | Описание
--- | ---
5.1.0 | Added the `options` parameter

## Примеры

Пример 1. Saving a DOM tree into a file

`<?php
$doc = new DOMDocument('1.0');
// we want a nice output
$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 'Wrote: ' . $doc->save("/tmp/test.xml") . ' bytes'; // Wrote: 72 bytes
?>`

## Смотрите также

DOMDocument->saveXML()
DOMDocument->load()
DOMDocument->loadXML()

add a note User Contributed Notes
DOMDocument->save()
siegparr at NOSPAM dot web dot de
07-Jul-2006 08:00
`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`
JimmyNighthawk
29-Oct-2005 02:52
`+++ EMERGENCY NOT +++
When the HTML-Specification which is implemented in ->saveHTML() is implemented in ->save() you are not able to store XML-Tags which have no End-Tags in HTML (e. g. <link>- and <meta>-Tags).
Same in other words:
When the HTML-Specification which is implemented in ->saveHTML() is accidently implemented in ->save() the VALUE and END-TAG of nodes will be removed/deleted, when there is no VALUE and END-TAG allowed in HTML.
If this happens note this to your Hoster. It should be corrected immediately to perfom full benefit of XML, and perhaps your XML-database.
Let us just say about dealed fairly: "shit happens"`
add a note
DOMDocument->saveHTML() DOMDocument->relaxNGValidateSource()

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST