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.
DOMDocument::save
(PHP 5)
DOMDocument::save — 内部の XML ツリーをファイルに出力する
説明
int DOMDocument::save
( string $filename
[, int $options
] )
DOM 表現から XML ドキュメントを作成します。この関数は、通常は以下の例のように DOM ドキュメントを新しく作成した後にコールされます。
返り値
書き込んだバイト数、あるいはエラーが発生した場合は FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.1.0 | options パラメータが追加されました。 |
例
例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
?>
参考
- DOMDocument::saveXML - 内部の XML ツリーを文字列として出力する
- DOMDocument::load - ファイルから XML を読み込む
- DOMDocument::loadXML - 文字列から XML を読み込む
DOMDocument::save
ss at littlerain dot com
01-Feb-2009 08:05
01-Feb-2009 08:05
chenel324 at gmail dot com
19-Oct-2008 01:38
19-Oct-2008 01:38
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.
jesdisciple at SPAM_SMELLS dot gmail dot com
01-Mar-2008 06:14
01-Mar-2008 06:14
@spoke_tire: This is common in Notepad, but you can fix it in each document - preferably in a text editor that takes line-breaks in the find/replace dialog. Copy that square character, put it in the Find field, then copy a real line-break and put it in the Replace field - or, if you only have WordPad for this, find each instance, click back to the titlebar, and press Ctrl+V. The latter can be very tedious, but I think it's sometimes worth it if Notepad is your default editor for TXT files.
Alternatively, get another text editor like Notepad++ or PSPad (although these have their own problem of deleting too much text if you use Undo/Redo too much) and make it your default for TXT files.
spoke_tire
21-Dec-2007 05:30
21-Dec-2007 05:30
When I use save(), I get these rectangular characters in the output file where there used to be line breaks. At least that's what it looks like in Notepad, but Wordpad treats the rectangles as line breaks.
jon at mysql dot com
02-Nov-2007 10:38
02-Nov-2007 10:38
If you're working with an existing XML file that contains an entity definition with an external reference like this one:
[
<!ENTITY % all.entities SYSTEM "all-entities.ent">
%all.entities;
]>
Be warned that DOMDocument->save() will expand this to include all definitions in all referenced entities file(s) when it writes the file. In other words, the same section of your saved file would now look something like this:
<!ENTITY % all.entities SYSTEM "all-entities.ent">
<!--
This file names all the entity files needed by .xml files in the
current directory. All ENTITY declarations should be given
first, followed by references to the those entities.
--><!ENTITY % fixedchars.entities SYSTEM "../common/fixedchars.ent">
<!ENTITY % urls.entities SYSTEM "../refman-common/urls.ent">
<!ENTITY % phrases.entities SYSTEM "../common/phrases.ent">
<!ENTITY % ndb.entities SYSTEM "ndb.en.ent">
<!ENTITY minus "–"><!-- 2013 is actually Unicode for ndash -->
<!ENTITY Oslash "Ø">
<!ENTITY macr "¯">
<!ENTITY auml "ä">
[etc.]
I've not found a way to keep this from occurring.
siegparr at NOSPAM dot web dot de
07-Jul-2006 03:00
07-Jul-2006 03: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 09:52
29-Oct-2005 09: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"
