【问题标题】:How add child node under a parent node in xml with php code如何使用php代码在xml中的父节点下添加子节点
【发布时间】:2015-01-22 07:07:55
【问题描述】:

这是在我的 xml 文件中:

<?xml version="1.0" ?> - <AXISWeb xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="AXISWeb.xsd"> <Membership> <ExpiryDate /> <MemStatus /> <MemStatusDesc /> <RenewType /> <!-- I want new node here with php code --> </Membership>

<Address> <address1/> ... </Address> &lt;/AXISWeb&gt; 请用其他语言而不是 php 给出这种类型的解决方案,任何人都可以为此提供帮助 谢谢。

【问题讨论】:

  • 我没有得到我想要的实际答案..

标签: php xml drupal


【解决方案1】:

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

<?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() 两次。
猜你喜欢
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-27
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
相关资源
最近更新 更多