PHP SimpleXML – create XML

05:08 PM by Christian Koch

Filed under php

Last modified Wednesday April 2nd, 2014

Here’s the snippet to create some xml with PHP’s SimpleXML Lib:

$phonebook = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><phonebook></phonebook>');

$contact = $phonebook->addChild('contact');

$person = $contact->addChild('person');
$person->addChild('LastName', 'Chris');
$person->addChild('FirstName', 'Koch');

$phone = $person->addChild('phone');
$phone->addChild('phonenumber', '0123456789');

$output = $phonebook->asXML();

If you want to directly save it to a file

$phonebook->asXML('/my/paht/phonebook.xml');

 

Related Articles

                                                          Leave a Comment