Open In App

PHP code to store XML data into CSV

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Requirements:

XAMPP Server.

Structure of XML:

<root>
  <child>
    <subchild>.....</subchild>
   </child>
</root>

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"?> 
<student> 
    <details> 
        <id>7058</id> 
        <name>G.sravan kumar</name> 
        <address>kakumanu</address>
    </details> 
    <details> 
        <id>7004</id> 
        <name>sudheer</name> 
        <address>usa</address>
    </details> 
    <details> 
        <id>7098</id> 
        <name>Rohith</name> 
        <address>hyd</address>
    </details> 
</student>

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.

Syntax:

file_exists(path/filename)
  • 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.

Syntax:

simplexml_load_file(file)
  • The file is the filename

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

Syntax:

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.

Syntax:

fclose(file_pointer)

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.

Syntax:

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 ( , )

Steps:

  • 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.

student_details.xml




<?xml version="1.0" encoding="UTF-8"?> 
<student
    <details
        <id>7058</id
        <name>G.sravan kumar</name
        <address>kakumanu</address>
    </details
    <details
        <id>7004</id
        <name>sudheer</name
        <address>usa</address>
    </details
    <details
        <id>7098</id
        <name>Rohith</name
        <address>hyd</address>
    </details
</student>


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

PHP




<?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
    fclose($i);
}
  
// 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



Last Updated : 07 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads