これは単純なはずですが、私はPHPには新しく、私が出会ったドキュメントは理解していません。私は簡単な説明が必要です。PHP appendChildからXMLへのルート
ノードを追加したいXMLドキュメントがあります。ドキュメントにノードを追加することはできますが、それらはルートノードの外にしか表示されず、後続の試行でエラーが発生します。ここ
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
と私のPHPは次のとおりです:
は、ここに私のXMLである
$playerID = "id_" . $_POST['player'];
//load xml file to edit
$xml = new DOMDocument();
$xml->load('../../saves/playerPositions.xml') or die("Error: Cannot load file.");
//check if the player is already in the database
if(isset($xlm->$playerID)){
echo "Player ID was found.";
}
else{
echo "Player ID was not found.";
//so we want to create a new node with this player information
$playerElement = $xml->createElement($playerID, "");
$playerName = $xml->createElement("name", "John Doe");
//add the name to the playerElement
$playerElement->appendChild($playerName);
//add the playerElement to the document
//THIS IS WHERE THE ERROR OCCURS
$xml->root->appendChild($playerElement);
//save and close the document
$xml->formatOutput = true;
$xml->save("../../saves/playerPositions.xml");
}
echo "done";
私はちょうど、私はドキュメントを変更することができます$xml->appendChild()
を使用しますが、新しいテキストが<root></root>
の外に表示された場合。
正確なエラーは、次のとおり$xml
はDOMDocument
のインスタンスであるため
Notice: Undefined property: DOMDocument::$root
をあなたの実際のコード、 '$ xml-> root - > ...'または '$ xml - > $ root - > ...'? – har07
$ xml-> root-> appendChild()を使用するとエラーはDOMDocument :: $ rootです $ xml - > $ root-> appendChild()を使用するとDOMDocument :: rootのエラーが発生します 私がそれをより正確に行う方法であると理解しているので、 "root"を使用しています。OPは私の正確なコードです。 – user1544522
しかし、エラーメッセージはコードに対応していないため、混乱します。 *致命的なエラー: 'null 'でメンバ関数' appendChild() 'を呼び出す* – har07