2012-02-09 4 views
2

내 문제는 php XML DOM 파서를 사용하여 저장할 때 내 xml 파일의 태그가 줄 바꿈 형식이 제대로 지정되지 않는다는 것이다. 여기 PHP 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); 

내 출력의 조각입니다 :

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

이 내 파일의 바로 결말이지만, 태그에 내가 대체거야 - topichead의 navtitle = "blarg blarg"를 함께 추가 된 topicref가 있으면 다음 행으로 이동하지 않고 옆에 tacked됩니다. 그리고 나는 이것을 이렇게 읽을 수는 없다.

위에서 보았 듯이 "$ xdoc-> formatOutput = true; $ xdoc-> preserveWhiteSpace = false;"를 시도했습니다.

그러나 이것들은 작동하지 않는 것처럼 보입니다. 탭 형식이지만, 올바른 줄 바꿈을 제공하지 않습니다.

감사합니다.)

답변

8

매우 일반적인 문제입니다. 이것은 약간의 짐승이지만 작동해야합니다 :

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

$xdoc->save($file);