SimpleXMLを介して、XMLファイルにレコードを書き込みますか?

ddbum:
$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データを追加する必要がありますか)?

u_mulder:

コードは次のようになります。

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

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=406730&siteId=1