2012-08-28 44 views
1

我正在写一个新的记录到XML文件与项目,一个英文单词和一个希伯来文单词。类DOMElement的错误对象无法转换为字符串

但行
$newWord->appendChild($prop.$new_line);
导致此错误 “类一个DOMElement的对象无法转换为字符串”

参数$ NEW_LINE等于$new_line = "\n";

我在这里错过什么thx?

我的代码是:

<?php 

    /*$wordH=$_GET['varHeb']; 
    $wordE=$_GET['varEng'];*/ 
    $wordH="newhebWord"; 
    $wordE="newengWord"; 
    $new_line = "\n"; 

$doc=''; 

     if(!$doc) 
     { 
      $doc = new DOMDocument(); 
      // we want a nice output 
      $doc->formatOutput = true; 
      $doc->load('Dictionary_user.xml'); 
     } 
     $Dictionary_user = $doc->documentElement; 

     $newWord = $doc->createElement('newWord'); 



     $prop = $doc->createElement('Heb', $wordH); 
     $newWord->appendChild($prop.$new_line); 
     $prop = $doc->createElement('Eng',$wordE); 
     $newWord->appendChild($prop.$new_line); 


     $Dictionary_user->childNodes->item(0)->parentNode->insertBefore($newWord,$Dictionary_user->childNodes->item(0)); 
     header("Content-type: text/xml"); 

     $doc->save("Dictionary_user.xml"); 
    echo $doc->saveXML(); 


    ?> 

回答

1

你并不需要附加一个换行符,你所处理的是实际数据结构(A DOMDocument)不是一个字符串。

+0

好吧,如果我想添加新行,这样文件将看起来不错我的壳我做的? ,它对我来说很重要! – yossi

+0

你已经在做:$ doc-> formatOutput = true; –

相关问题