2011-12-27 57 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 2011-12-27 18:31:19

回答

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