How to Create and Save XML File using PHP Tutorial

Share this Article
Reading Time: 4 minutes
2,293 Views

Inside this article we will see how to create and save XML file using PHP. This contains a classified information about generating XML file. Very simple steps you need to do and create your own XML using dynamic data too.

In this article we will consider an array of data. We will convert that array of data into XML file and save into application. This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well.

We will use some basic PHP functions to create and save XML file in PHP. PHP class SimpleXMLElement will helps to generate XML file.

Learn More –

Let’s get started.


Create an Application

Create a folder php-xml in your localhost directory. Make sure this folder should have sufficient permissions to read and write operations.

Inside this folder, create a file called index.php.

<?php
// employees data
$employees = [
    [
        "name" => "Sanjay Kumar",
        "designation" => "Web Developer",
        "website" => "https://abc.com"
    ],
    [
        "name" => "Vijay Rohila",
        "designation" => "Python Developer",
        "website" => "https://xyz.com"
    ],
    [
        "name" => "Ashish Kumar",
        "designation" => "Laravel Developer",
        "website" => "https://abcxyz.com"
    ],
    [
        "name" => "Dhananjay Negi",
        "designation" => "Full Stack Developer",
        "website" => "https://stack.xyz"
    ]
];

// array to xml function
function array_to_xml($data, &$xml_data)
{
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            if (is_numeric($key)) {
                $key = 'employee';
            }
            $subnode = $xml_data->addChild($key);
            array_to_xml($value, $subnode);
        } else {
            $xml_data->addChild("$key", htmlspecialchars("$value"));
        }
    }
}

// creating object of SimpleXMLElement
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><employees></employees>');

// function call to convert array to xml
array_to_xml($employees, $xml_data);

//saving generated xml file; 
$result = $xml_data->asXML('employees.xml');

Here,

A PHP class SimpleXMLElement has been used to create a XML instance. <employees></employees> is the root element for data.

array_to_xml($employees, $xml_data);

Above function is user defined function created to make array data into XML data.

To save generated XML file into application we used asXML(). employees.xml is the filename.

$result = $xml_data->asXML('employees.xml');

Application Testing

Open browser and hit this URL: http://localhost/php-xml

It will create a file employees.xml in same folder. File looks like this.

We hope this article helped you to learn How to Create and Save XML File using PHP Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5