Convert Associative Array into XML in PHP
XML (Extensible Markup Language) is a markup language that encodes documents in a machine-readable and human-readable format. When we have an issue with database size to store dynamic data then XML file format is the best to store dynamic data and retrieve it according to needs.
In this, tutorial we will data in associative array and we will create XML data from associative array and write to XML file. This is a very simple example, you can just copy paste, and change according to your requirement.
Step 1: Write XML Data File from Associative Array
Create a function generateXMLFile() 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php function generateXMLFile($studentData) { $title = $studentData['title']; $totalStudent = count($studentData['student']); //create the xml document $xmlDocument = new DOMDocument(); $root = $xmlDocument->appendChild($xmlDocument->createElement("student_details")); $root->appendChild($xmlDocument->createElement("title",$title)); $root->appendChild($xmlDocument->createElement("totalStudent",$totalStudent)); $studentRecords = $root->appendChild($xmlDocument->createElement('student_records')); foreach($studentData['student'] as $student){ if(!empty($student)){ $studentRecord = $studentRecords->appendChild($xmlDocument->createElement('student')); foreach($student as $key=>$val){ $studentRecord->appendChild($xmlDocument->createElement($key, $val)); } } } //save xml file $fileName = str_replace(' ', '_',strtolower($title)).'-'.time().'.xml'; //make the output pretty $xmlDocument->formatOutput = true; $xmlDocument->save("xml_file/" . $fileName); //return xml file name return $fileName; } ?> |
Step 2: Store Data into Associative Array
Create associative array as $studentData to convert array data to XML data.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $studentData = array( 'title' => 'Student Details', 'student' => array( array('name' => 'Tom', 'email' => 'topm@techarise.com', 'age'=>'15'), array('name' => 'Jerry', 'email' => 'jerry@techarise.com', 'age'=>'17'), array('name' => 'James', 'email' => 'james@techarise.com', 'age'=>'14'), array('name' => 'Wood', 'email' => 'wood@techarise.com', 'age'=>'16'), array('name' => 'Adam', 'email' => 'adam@techarise.com', 'age'=>'18') ) ); ?> |
Step 3: Convert Associative Array onto XML
Now we will call function generateXMLFile() and pass associative array $studentData to convert into XML file. The file will be stored into “xml_file” directory.
1 2 3 4 |
<?php $file_name = generateXMLFile($studentData); echo '<a target="_blank" href="http://localhost/ta/convert-associative-array-into-xml-in-php/xml_file/'.$file_name.'" rel="noopener noreferrer">'.$file_name.'</a>'; ?> |
Sample output of the above code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0"?> <student_details> <title>Student Details</title> <totalStudent>5</totalStudent> <student_records> <student> <name>Tom</name> <email>tom@techarise.com</email> <age>15</age> </student> <student> <name>Jerry</name> <email>jerry@techarise.com</email> <age>17</age> </student> <student> <name>James</name> <email>james@techarise.com</email> <age>14</age> </student> <student> <name>Wood</name> <email>wood@techarise.com</email> <age>16</age> </student> <student> <name>Adam</name> <email>adam@techarise.com</email> <age>18</age> </student> </student_records> </student_details> |