array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'ja', ), 'this' => array ( 0 => 'domdocument.save.php', 1 => 'DOMDocument::save', ), 'up' => array ( 0 => 'class.domdocument.php', 1 => 'DOMDocument', ), 'prev' => array ( 0 => 'domdocument.replacechildren.php', 1 => 'DOMDocument::replaceChildren', ), 'next' => array ( 0 => 'domdocument.savehtml.php', 1 => 'DOMDocument::saveHTML', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'ja', 'path' => 'reference/dom/domdocument/save.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5, PHP 7, PHP 8)
DOMDocument::save — 内部の XML ツリーをファイルに出力する
DOM 表現から XML ドキュメントを作成します。この関数は、通常は以下の例のように DOM ドキュメントを新しく作成した後にコールされます。
書き込んだバイト数、あるいはエラーが発生した場合は false
を返します。
例1 DOM ツリーをファイルに保存する
<?php
$doc = new DOMDocument('1.0');
// 出力はきれいに整形したいですね。
$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
?>