PHP DOM XML不会在标记之后打印掉换行符

So my problem is that the tags in my xml file aren't being properly formatted with line breaks after them when I use the php XML DOM parser to save.

$xdoc = new DOMDocument();
$xdoc->formatOutput = true;
$xdoc->preserveWhiteSpace  = false;
$xdoc->load($file);

$new_topic=$xdoc->createElement("topicref", "");
$new_topic->setAttribute("navtitle", $new_node);

$new_topichead=$xdoc->createElement("topichead", "");
$new_topichead->setAttribute("navtitle", $parent_node->getAttribute("navtitle"));
$new_topichead->appendChild($new_topic);
$parent_node->parentNode->replaceChild($new_topichead, $parent_node);

$xdoc->save($file);

Here's a snippet of my output:

<topichead>
    <topichead navtitle="blarg blarg"><topicref navtitle="another blarg blarg" href="another blarg blarg"></topicref></topichead>
</topichead>

This is just the ending of my file, but for the tag i'm replacing - the topichead navtitle="blarg blarg", with the appended topicref, it gets tacked on next to it rather than going to the next line. And I can't read it like this.

As you see above I have tried " $xdoc->formatOutput = true; $xdoc->preserveWhiteSpace = false;"

but these don't seem to work - they format with the tabs, but it's not giving me the right line breaks.

Thanks =)

It's a common issue, very annoying. This is a bit brute-force, but it should work:

// Reload XML to cause format output and/or preserve whitespace to take effect
$xdoc->loadXML($xdoc->saveXML());

$xdoc->save($file);