How to Create and Save XML File using PHP Tutorial

Reading Time: 4 minutes
8,073 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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more