2011-12-27 11 views
1
<?xml version="1.0" encoding="ISO-8859-1"?> 
<messages> 
<post> 
<id>1</id> 
<userName>lala</userName> 
<text>some nice text</text> 
<timePosted>12456754</timePosted> 
</post> 
</messages> 

新しい投稿をメッセージに追加します。どのようにしますか? addChildで追加しようとしましたが、うまくいきませんでした。新しい投稿をXMLファイルに追加します

+2

現在のコード/試行を表示してください。また、 'addChild'では何が「うまくいきませんでしたか」? – ziesemer

答えて

2
// Load the XML file 
$xml = simplexml_load_file($filename); 

// Create a new child node 
$child = $xml->addChild('post'); 
$child->addChild('id', $id); 
$child->addChild('userName', $username); 
$child->addChild('text', $text); 
$child->addChild('timePosted', $timeposted); 

// Save the updated XML back to the file 
$xml->asXML($filename); 
関連する問題