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.
Step 2: Store Data into Associative Array
Create associative array as $studentData to convert array data to XML data.
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.
Sample output of the above code.