2011-08-15 7 views
0

このスクリプトは元のデータで新しいXMLファイルを作成し、更新は行いません。PHPスクリプトは更新なしでxmlを保存します

<?php 

$dom = new DOMDocument(); 
$dom->load('communities.xml'); 

$dom->preserveWhiteSpace = false; 
$dom->formatOutput = true; 

// get document element 

$root = $dom->documentElement; 

$name  = $dom->createElement("NAME"); 
$nameText = $dom->createTextNode("Hobbies"); 
$name->appendChild($nameText); 

$community = $dom->createElement('COMMUNITY'); 
$community->appendChild($name); 
$dom->save('communities.xml'); 

$tidy_options = array(
       'input-xml' => true, 
       'output-xml' => true, 
       'indent'  => true, 
       'wrap'   => false, 
     ); 
$tidy = new tidy(); 
$tidy->parseFile('communities.xml', $tidy_options); 
$tidy->cleanRepair(); 
echo $tidy; 

return; 

?> 

ファイルの形式が変更され、ファイルが新しい日付で保存されるため、アクセス許可が適切です。

+0

ここでの質問は正確ですか? –

答えて

1

それはあなたには、いくつか、すなわち、ステートメントを追加欠けているように見えます:

$root->appendChild($name); 
... 
$root->appendChild($community); 
+0

はい、私は '$ root-> appendChild($ community);'がありませんでした。ありがとう。 – user823527

0

ファイルが変更され、新しい日付が設定されている可能性がありますが、2回の保存操作を行っているだけで、実際に何かを行っているのはたやすいものでしょう。試してみてください:

if ($dom->save('communites.xml') === FALSE) { 
    die(libxml_get_errors()); 
} 

which'll出力任意のDOMエラー

0

あなたは要素を作成した後、あなたがそれらを追加する必要がありますドキュメント:

$community = $dom->createElement('COMMUNITY'); 
$root->appendChild($community); 
関連する問題