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


How add child node under a parent node in xml with php code

这是我的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> </AXISWeb>请以其他语言但不以 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');
    ?>