通过简单xml将记录写入xml文件?

write record into xml file via simplexml?

$xmldbase = simplexml_load_file($xmlfile);
$date = date();

$id = (integer) $xmldbase['id'];
$xmldbase['id'] = $id+1;

$xmlString = "
    <player>
        <id>$id</id>
        <name>$name</name>
        <date>$date</date>
        <score>$score</score>
        <right>$rightA</right>
        <false>$falseA</false>
        <ip>$ip</ip>
    </player>";

$xmldbase = new SimpleXMLElement($xmlString);

$xmldbase->asXML($xmlfile);

这到目前为止有效,但没有将节点播放器添加到我的文件中,它被覆盖了。但是我如何添加我的玩家节点呢?该示例未加载文件(https://www.php.net/manual/de/simplexmlelement.addchild.php) 应在何处添加数据?

代码类似于:

$xmldbase = simplexml_load_file($xmlfile);
$date = date();

$id = (integer) $xmldbase['id'];
$xmldbase['id'] = $id+1;

// add new `player` node to root xml-element
$player = $xmldbase->addChild('player');
// add `id` node to just created `player` node
$player->addChild('id', $id);
// add `name` node to just created `player` node
$player->addChild('name', $name);
// add other nodes here

$xmldbase->asXML($xmlfile);