Convert Associative Array into XML in PHP

XML (Extensible Markup Language) is a common markup language which is used in web applications to read and write dynamic data. When we have issue with database size to store dynamic data then XML file format is the best to store dynamic data and retrieve according to needs.

So if you’re thinking about storing your web application data into XML instead of database then you’re here right place. In our previous tutorial you have learned how to convert XML into associative array in PHP. n this tutorial you will learn how to write your dynamic data to XML file for further use.

Also, read:

Here in this tutorial we will data in associative array and we will create XML data from associative array and write to XML file.

Create XML from Associative Array in PHP

Here we will learn how to create XML data from associative array.


Step1: Write XML Data File from Associative Array
First we will create a function createXMLFile() to implement functionality to create XML from associative array. We will use PHP library class DOMDocument() to create XML data and save into XML file. We will create directory XML_FILE in project folder to store XML file.

<?php
function createXMLFile($empData) {
    $title = $empData['title'];
    $totalEmployee = count($empData['employee']);    
    $xmlDocument = new DOMDocument();    
    $root = $xmlDocument->appendChild($xmlDocument->createElement("employee_details"));
    $root->appendChild($xmlDocument->createElement("title",$title));
    $root->appendChild($xmlDocument->createElement("totalEmployee",$totalEmployee));
    $empRecords = $root->appendChild($xmlDocument->createElement('emp_records'));    
    foreach($empData['employee'] as $employee){
        if(!empty($employee)){
            $empRecord = $empRecords->appendChild($xmlDocument->createElement('employee'));
            foreach($employee as $key=>$val){
                $empRecord->appendChild($xmlDocument->createElement($key, $val));
            }
        }
    }         
    $fileName = str_replace(' ', '_',$title).'_'.time().'.xml';
	$xmlDocument->formatOutput = true;  
    $xmlDocument->save("XML_FILE/" . $fileName);        
}
?>

Step2: Store Data into Associative Array
We will create associative array as $empData to convert array data to XML data.

$empData = array(
	'title' => 'Employee Details',
	'employee' => array(
		array('name' => 'William Smith', 'email' => 'william@phpzag.com'),
		array('name' => 'Andy Flower', 'email' => 'andy@phpzag.com'),
		array('name' => 'Cris Jhon', 'email' => 'jhon@phpzag.com')
	)
);

Step3: Convert Associative Array onto XML
Now we will call function createXMLFile() and pass associative array $empData to convert into XML file. The file will be stored into XML_FILE directory.

echo createXMLFile($empData);

The above code will create the following XML data.

<?xml version="1.0"?>
<employee_details>
  <title>Employee Details</title>
  <totalEmployee>3</totalEmployee>
  <emp_records>
    <employee>
      <name>William Smith</name>
      <email>william@phpzag.com</email>
    </employee>
    <employee>
      <name>Andy Flower</name>
      <email>andy@phpzag.com</email>
    </employee>
    <employee>
      <name>Cris Jhon</name>
      <email>jhon@phpzag.com</email>
    </employee>
  </emp_records>
</employee_details>

You may also like:


Leave a Reply

Your email address will not be published. Required fields are marked *