2011-12-27 97 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);