2016-10-02 77 views
0

我知道這應該很簡單,但我是PHP新手,只是不理解我遇到的文檔。我需要一個簡單的解釋。PHP appendChild到XML根

我有一個XML文檔,我想添加節點。我可以將節點添加到文檔中,它們只出現在根節點之外,並在隨後的嘗試中導致錯誤發生。

這裏是我的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
</root> 

,這裏是我的PHP:

$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>之外。

確切的錯誤是:

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

好的,但錯誤信息不符合代碼,因此它是混亂。我用'$ xml-> root'得到的錯誤信息:*致命錯誤:調用'null'上的成員函數'appendChild()'* – har07

回答

1

$xml->root不訪問根元素在此背景下,正確的方法,因爲$xmlDOMDocument一個實例(它會工作,如果$xmlSimpleXMLElement代替)。您可以從documentElement財產得到DOMDocument對象的根元素:

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

eval.in demo 1

,或者更一般地說,可以使用getElementsByTagName()度日name元素:

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

此代碼生成:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
<player><playerName>Jhon Doe</playerName></player></root>