2016-10-02 6 views
0

これは単純なはずですが、私は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>の外に表示された場合。

正確なエラーは、次のとおり$xmlDOMDocumentのインスタンスであるため

Notice: Undefined property: DOMDocument::$root

+0

をあなたの実際のコード、 '$ xml-> root - > ...'または '$ xml - > $ root - > ...'? – har07

+0

$ xml-> root-> appendChild()を使用するとエラーはDOMDocument :: $ rootです $ xml - > $ root-> appendChild()を使用するとDOMDocument :: rootのエラーが発生します 私がそれをより正確に行う方法であると理解しているので、 "root"を使用しています。OPは私の正確なコードです。 – user1544522

+0

しかし、エラーメッセージはコードに対応していないため、混乱します。 *致命的なエラー: 'null 'でメンバ関数' appendChild() 'を呼び出す* – har07

答えて

1

$xml->rootは($xml代わりSimpleXMLElementた場合、それが動作する)、この文脈では、ルート要素にアクセスするための正しい方法ではありません。あなたはdocumentElementプロパティからDOMDocumentオブジェクトのルート要素を取得することができます:

$xml->documentElement->appendChild($playerElement); 

eval.in demo 1

あるいは、より一般的に、あなたはgetElementsByTagName()使用して、名前で要素を取得することができます:

$xml->getElementsByTagName("root")->item(0)->appendChild($playerElement); 

eval.in demo 2

さらに読書:(?プラスタイプミス)PHP DOM: How to get child elements by tag name in an elegant manner?


あなたのコードのこの部分は、同じ理由でも正しくない、言った:

if(isset($xlm->$playerID)){ 
    echo "Player ID was found."; 
} 

getElementsByTagName()と交換:

if($xml->getElementsByTagName($playerID)->length > 0){ 
    echo "Player ID was found."; 
} 
+0

コードを開始したとき、私はSimpleXMLElementを使用しようとしていましたが、ファイルを読むのを手伝ってください。私はDOMDocumentで終わりましたが、それは別の構造であることを認識しませんでした。 この構文は長くなっていますが、JavaScriptとXMLのような理由から、この構文はより快適です。ご協力いただきありがとうございます。 – user1544522

0

あなたはStandarオブジェクトのようにdomを処理しようとしていますが、そうではありません。 要素を探すために、あなたが機能getElementsByTagNameのを使用しており、このようなノードのコレクション、などのDOMを処理する必要があります。

$xml = new DOMDocument(); 
    $xml->loadXML('<?xml version="1.0" encoding="UTF-8"?> 
<root> 
</root>') or die("Error: Cannot load file."); 

    $len = $xml->getElementsByTagName('player')->length; 
    if ($len > 0) { 

    } else { 
     $player = $xml->createElement('player', ''); 
     $playerName = $xml->createElement('playerName', "Jhon Doe"); 
     $player->appendChild($playerName); 

     $root = $xml->getElementsByTagName('root'); 

     if ($root->length > 0) { 
     $root[0]->appendChild($player); 
     } 

     $xml->formatOutput = true; 
     echo $xml->saveXML(); 
    } 

このコードが生成する:

あなたが持っていない1
<?xml version="1.0" encoding="UTF-8"?> 
<root> 
<player><playerName>Jhon Doe</playerName></player></root> 
関連する問題