使用php在xml中插入节点

时间:2009-10-24 16:57:25

标签: 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>

我希望使用php为另外一名员工插入数据。

此致 新手

2 个答案:

答案 0 :(得分: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());

答案 1 :(得分:2)

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