How to create XML files

If you need to create XML files in PHP, you can do it without having to create the tags yourself with strings. This code shows you how to use the new_child functions to create an XML file.

$doc = new_xmldoc('1.0');
$root = $doc->add_root('members');
$member = $root->new_child('member','');


$member = $root->new_child('member','');


$member = $root->new_child('member','');


$fp = @fopen('members.xml','w');
if(!$fp) {
    die('Error cannot create XML file');

would create the following XML file.


You might also like...


Edward Tanguay Edward Tanguay updates his personal web site weekly with code, links, quotes and thoughts on web development. Sign up for the free newsletter.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout