php domdocument在一行

时间:2013-01-21 06:39:16

标签: php xml domdocument

我正在尝试使用PHP编程语言创建一个带衬里的XML String。

我正在使用DomDocument。

我将preserveWhitespace设置为false,将formatOutput设置为false,但是当我执行时 saveXML()输出仍然显示每个XML输出之间的新行。任何想法?

我的代码:

            $domtree = new \DOMDocument();

            $domtree->preserveWhiteSpace = false;
            $domtree->formatOutput = false;
            ...

1 个答案:

答案 0 :(得分:2)

尝试以下方法:

$dom    = new DOMDocument( '1.0' );
$dom->loadXML( '<nodes><test/></nodes>', LIBXML_NOBLANKS | LIBXML_COMPACT );
$dom->preserveWhiteSpace = false;
$dom->formatOutput  = false;
// remove all new lines
$XmlFileText = preg_replace("/\n/", "", $dom->saveXML());
echo $XmlFileText;

不是最好的方式,但有效。希望它有所帮助。