5

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

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

1 Comment

what if the XML is held as a variable? oh, its ok: simplexml_load_string. Should've checked first before asking
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.