2016-10-02 30 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>