Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert node in xml using php

Tags:

php

xml

I want to insert text node and create element in xml using php for example

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>

I want insert Data for one more employees using php.

Regards NewBie

like image 681
NewBie Avatar asked Oct 24 '09 16:10

NewBie


2 Answers

<?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());
like image 53
CodeJoust Avatar answered Sep 22 '22 17:09

CodeJoust


See the DOMDocument class documentation. There are examples for XML parsing and modifying.

like image 29
Lukáš Lalinský Avatar answered Sep 18 '22 17:09

Lukáš Lalinský