Добавить новое сообщение в файл XML - PullRequest
1 голос
/ 27 декабря 2011
<?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, не сработало.

1 Ответ

2 голосов
/ 27 декабря 2011
// 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);
...