【问题标题】:PHP DOM XML not printing out line breaks after tagsPHP DOM XML没有在标签后打印换行符
【发布时间】:2012-03-02 08:13:29
【问题描述】:

所以我的问题是,当我使用 php XML DOM 解析器进行保存时,我的 xml 文件中的标签没有正确格式化,并在它们后面加上换行符。

$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);

这是我的输出的 sn-p:

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

这只是我的文件的结尾,但是对于我要替换的标签 - topichead navtitle="blarg blarg",带有附加的 topicref,它被添加到它旁边而不是转到下一行。而且我不能这样读。

正如你在上面看到的,我已经尝试过“ $xdoc->formatOutput = true; $xdoc->preserveWhiteSpace = false;"

但这些似乎不起作用 - 它们使用标签格式化,但它没有给我正确的换行符。

谢谢 =)

【问题讨论】:

    标签: php xml dom domdocument


    【解决方案1】:

    这是一个常见的问题,非常烦人。这有点蛮力,但它应该工作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多