Create and Save XML File using DOMDocument in PHP

Reading Time: 4 minutes
4,327 Views

Inside this article we will see how to create and save XML file using DOMDocument in 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 DOMDocument 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"
    ]
];

/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('1.0', 'UTF-8');

/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("employees");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);

// iterate over data
foreach ($employees as $employee) {
    $subnode = $domtree->createElement("employee");
    $subnode = $xmlRoot->appendChild($subnode);
    if (is_array($employee)) {
        $empKeys = array_keys($employee);
        foreach ($empKeys as $emp) {
            $subnode->appendChild($domtree->createElement($emp, $employee[$emp]));
        }
    }
}

/* save the xml */
$domtree->save("employees.xml");

Here,

Create an instance of DOMDocument

$domtree = new DOMDocument('1.0', 'UTF-8');

Create a root element of XML

$xmlRoot = $domtree->createElement("employees");

Read all array data and create XML element.

// iterate over data
foreach ($employees as $employee) {
    $subnode = $domtree->createElement("employee");
    $subnode = $xmlRoot->appendChild($subnode);
    if (is_array($employee)) {
        $empKeys = array_keys($employee);
        foreach ($empKeys as $emp) {
            $subnode->appendChild($domtree->createElement($emp, $employee[$emp]));
        }
    }
}

Save XML file

$domtree->save("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 DOMDocument in 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.