Open In App

PHP code to store XML data into CSV

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&gt
    <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)

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

Syntax:

simplexml_load_file(file)

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

Syntax:

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

Steps:

XML data: The following is the content for the file 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
  
// 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


Article Tags :