【问题标题】:PHP appendChild to XML rootPHP appendChild 到 XML 根
【发布时间】:2016-10-02 04:35:05
【问题描述】:

我知道这应该很简单,但我是 PHP 新手,只是不了解我遇到的文档。我需要一个简单的解释。

我有一个想要添加节点的 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-&gt;appendChild(),那么我可以修改文档,但新文本出现在&lt;root&gt;&lt;/root&gt;之外。

确切的错误是:

注意:未定义的属性:DOMDocument::$root

【问题讨论】:

  • 您的实际代码中有哪一个,$xml-&gt;root-&gt;...$xml-&gt;$root-&gt;...
  • 如果我使用 $xml->root->appendChild() 那么错误是 DOMDocument::$root 如果我使用 $xml->$root->appendChild() 那么错误是 DOMDocument: :root 但我使用的是“root”,因为我知道这是更正确的做法。 OP 是我的确切代码。
  • 好的,但是错误信息与代码不对应,因此令人困惑。我收到$xml-&gt;root 的错误消息:致命错误:在null 上调用成员函数appendChild()

标签: php xml appendchild


【解决方案1】:

$xml-&gt;root 不是在这种情况下访问根元素的正确方法,因为$xmlDOMDocument 的一个实例(如果$xmlSimpleXMLElement 则它会起作用)。您可以从 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.";
}

【讨论】:

  • 当我启动代码时,我试图使用 SimpleXMLElement,但无法让它读取文件。我最终得到了 DOMDocument,没有意识到它是一个不同的结构。虽然这种语法更长,但我更喜欢它,因为它类似于 JavaScript 和 XML。感谢您的帮助。
【解决方案2】:

您正试图像处理标准对象一样处理 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>

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    相关资源
    最近更新 更多