When inserting XML DOM Elements inside existing XML DOM Elements that I loaded from an XML file using the following code, none of my new elements were formatted correctly, they just showed up on one line:
<?php
$dom = DOMDocument::load('file.xml');
$dom->formatOutput = true;
//$dom->add some new elements with child nodes somewhere inside the loaded XML using insertBefore();
$dom->saveXML();
//output: everything looks normal but the new nodes are all on one line.
?>
I found I could pass LIBXML_NOBLANKS to the load method and it would reformat the whole document, including my added stuff:
<?php
$dom = DOMDocument::load('file.xml', LIBXML_NOBLANKS);
$dom->formatOutput = true;
//$dom->add some new elements with child nodes somewhere inside the loaded XML using insertBefore();
$dom->saveXML();
//output: everything looks newly formatted, including new nodes
?>
Hope this helps, took me hours of trial and error to figure this out!
定義済み定数
以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。
- LIBXML_COMPACT (integer)
-
小さなノードを割り当てるように最適化する。アプリケーションの
コードを変更することなしにスピードアップさせることができる
注意:
Libxml >= 2.6.21 でのみ有効
- LIBXML_DTDATTR (integer)
- デフォルトのDTD属性
- LIBXML_DTDLOAD (integer)
- 外部サブセットをロードする
- LIBXML_DTDVALID (integer)
- DTDで検証する
- LIBXML_NOBLANKS (integer)
- 空白のノードを削除
- LIBXML_NOCDATA (integer)
- CDATA をテキストノードとしてマージ
- LIBXML_NOEMPTYTAG (integer)
-
空タグを拡張する(例 <br/> を
<br></br> にする)
注意:
このオプションは、現在 DOMDocument::save および DOMDocument::saveXML 関数でのみ有効です。
- LIBXML_NOENT (integer)
- エンティティを置換
- LIBXML_NOERROR (integer)
- エラー出力を抑制
- LIBXML_NONET (integer)
- ドキュメントロード時にネットワークアクセスを無効にする
- LIBXML_NOWARNING (integer)
- 警告出力を抑制する
- LIBXML_NOXMLDECL (integer)
-
ドキュメントの保存時に XML 宣言を削除する
注意:
Libxml >= 2.6.21 でのみ有効
- LIBXML_NSCLEAN (integer)
- 冗長な名前空間宣言を削除する
- LIBXML_PARSEHUGE (integer)
-
パーサでハードコーディングされたすべての制限を緩和するための
XML_PARSE_HUGE フラグを設定する。
これは、ドキュメントやエンティティの再帰の最大数や
テキストノードのサイズなどの制限に影響する。
注意:
Libxml >= 2.7.0 (PHP >= 5.3.2 あるいは PHP >= 5.2.12) でのみ有効
- LIBXML_XINCLUDE (integer)
- XInclude 置換を実装する
- LIBXML_ERR_ERROR (integer)
- 復帰可能なエラー
- LIBXML_ERR_FATAL (integer)
- 致命的なエラー
- LIBXML_ERR_NONE (integer)
- エラーなし
- LIBXML_ERR_WARNING (integer)
- 単純な警告
- LIBXML_VERSION (integer)
- 20605 や 20617 のような libxml のバージョン
- LIBXML_DOTTED_VERSION (string)
- 2.6.5または2.6.17のようなlibxmlのバージョン
@oneseventeen
05-Feb-2011 11:51
zachatwork at gmail dot com
09-Feb-2010 11:30
Note: The LIBXML_NOXMLDECL constant is defined in this library but is not supported by DOMDocument (yet).
See also: http://bugs.php.net/bug.php?id=47137
<?php
print "PHP_VERSION: ".PHP_VERSION."\n";
print "LIBXML_VERSION: ".LIBXML_VERSION."\n";
print "LIBXML_NOXMLDECL: ".LIBXML_NOXMLDECL."\n";
$dom = new DomDocument();
$dom->loadXML("<foo />");
# This should work but doesn't.
print "DOMDocument doesn't honor LIBXML_NOXMLDECL:\n";
print $dom->saveXML(null,LIBXML_NOXMLDECL);
# This works, and will still work after the above is fixed.
print "Forwards compatible workaround:\n";
$lines = explode("\n", $dom->saveXML(null, LIBXML_NOXMLDECL), 2);
if(!preg_match('/^\<\?xml/', $lines[0]))
print $lines[0];
print $lines[1];
?>
PHP_VERSION: 5.3.1-0.dotdeb.1
LIBXML_VERSION: 20632
LIBXML_NOXMLDECL: 2
DOMDocument doesn't honor LIBXML_NOXMLDECL:
<?xml version="1.0"?>
<foo/>
Forwards compatible workaround:
<foo/>
