php domdocument在一行

I'm trying to create an one lined XML String using PHP programming language.

I'm using a DomDocument for that.

i'm setting preserveWhitespace to false and formatOutput to false but when i execute saveXML() theoutput still shows me new lines betweeen each XML output. any ideas?

my code:

            $domtree = new \DOMDocument();

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

Try the following:

$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("/
/", "", $dom->saveXML());
echo $XmlFileText;

Not the best way, but works. Hope it helps.