使用php在xml中插入节点

New*_*Bie 5 php xml

我想插入文本节点并使用php在xml中创建元素

XML

<?xml version="1.0"?>
<employees>
  <employee>
    <name>Albert</name>
    <age>34</age>
    <salary>$10000</salary>
  </employee>
  <employee>
    <name>Claud</name>
    <age>20</age>
    <salary>$2000</salary>
  </employee>
</employees>
Run Code Online (Sandbox Code Playgroud)

我想使用php为另外一名员工插入数据.

关心NewBie

Cod*_*ust 16

<?php 
$xml = simplexml_load_file('clients.xml');
$employee = $xml->addChild('employee');
$employee->addChild('name', 'Claud');
$employee->addChild('age', '20');
$employee->addChild('salary', 'This is all about the people who make it work.');

file_put_contents('clients.xml', $xml->asXML());
Run Code Online (Sandbox Code Playgroud)


Luk*_*ský 2

请参阅DOMDocument类文档。有XML解析和修改的例子。