如何使用php代码在xml中的父节点下添加子节点

如何使用php代码在xml中的父节点下添加子节点,php,xml,drupal,Php,Xml,Drupal,这在我的xml文件中: - ... 这种类型的解决方案是用其他语言给出的,但不是用php。有人能帮上忙吗 谢谢。以下是解决您问题的方法: <?php $xmldoc = new DOMDocument(); //xmldata.xml contains your xml data. $xmldoc->load('xmldata.xml'); //if you want to add child under AXISWeb node

这在我的xml文件中:

-

... 这种类型的解决方案是用其他语言给出的,但不是用php。有人能帮上忙吗
谢谢。

以下是解决您问题的方法:

<?php
     $xmldoc = new DOMDocument();
     //xmldata.xml contains your xml data.
     $xmldoc->load('xmldata.xml');
     //if you want to add child under AXISWeb node
     $root = $xmldoc->firstChild;
     //otherwise use this line
    //index = index of parent node such as for "Membership" it is 0. So just  replace index with 0.
    $root=xmlDoc.getElementsByTagName("AXISWeb")[index];
    $newElement = $xmldoc->createElement('newchild');
    $root->appendChild($newElement);
    $xmldoc->save('xmldata.xml');
    ?>

我没有得到我想要的实际答案..看起来你的代码被复制/粘贴错了$newElement在定义之前使用,您可以使用appendChild两次。