Open In App

PHP code to store XML data into CSV

Last Updated : 07 Jul, 2021
Like Article

In this article, we are going to store XML data into a CSV file using PHPH. XML stands for an extensible markup language. XML is similar to HTML but whereas in HTML, we can’t define our own tags, but in XML we can define our own tags.


XAMPP Server.

Structure of XML:


Example: We are taking student details saved as student_data.xml file and store this data in the CSV file.

<?xml version="1.0" encoding="UTF-8"?> 
        <name>G.sravan kumar</name> 

CSV: CSV stands for comma-separated value.

We are going to use some predefined functions to store XML data into CSV. The file_exists() function is used to check whether a file or directory exists.


  • The path is the path where the file exists.
  • The filename is the name of the file.

simplexml_load_file() function: The simplexml_load_file() function is used to convert an XML document to an object.


  • The file is the filename

fopen() function: It is used to open a file.


fopen(filename, mode)
  • The filename is the name of the file.
  • The modes are a (append) ,r (read mode), w (write mode)

fclose() function: This function is used to close the opened file.



fputcsv() Function: The fputcsv() function is used to format a line as CSV(comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the length of the written string on success or FALSE on failure.


fputcsv(file, fields, separator)
  • file: It is used to specify the opened file to write to.
  • fields: It is required to specify which array to get the data from.
  • separator: It is a character that specifies the field separator. Default is comma ( , )


  • Start XAMPP server.

  • Open notepad and create XML code and save it as student_details.xml in xampp/htdocs/geek folder.
  • Open notepad and create PHP code and save it as xml_code.php in xampp/htdocs/geek folder. 

XML data: The following is the content for the file student_details.xml.


<?xml version="1.0" encoding="UTF-8"?> 
        <name>G.sravan kumar</name

PHP code: The following is the content for the file xml_code.php


// Open xml file that is present in
// your folder
$xmldata = 'student_details.xml';
// Check if your file mentioned above
// is exists or not
if (file_exists($xmldata)) {
    // If file exists then load your xml
    // data using simplexml_load_file
    // function
    $xml_data = simplexml_load_file($xmldata);
    // Open xml file using fopen in write
    // mode and download the data as
    // result.csv
    $i = fopen('result.csv', 'w');
    // Function call 
    Csv($xml_data, $i);
    // Closing file
// Function to create csv file
function Csv($xml_data, $i) {
    // Count data for data present in
    // xml using children function
    foreach ($xml_data->children() as $data) {
        $hasChild = (count($data->children()) 
                        > 0) ? true : false;
        // Data is present, then store into
        // csv by using fputcsv function
        if( ! $hasChild) {
            $arr = array($data->getName(),$data); 
            fputcsv($i, $arr ,',','"');
        else {
            // Call function
            Csv($data, $i);

Open your browser and type localhost/geek/xml_code.php path in address bar.

Go to your folder and you will see the CSV file has been created.

Output: When the CSV file is opened, the following output is shown.


CSV file

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads