PHP 

Introduction

The example, convert xml to csv using PHP , shows conversion of xml data to csv data. As the title suggests to convert xml to csv using PHP, so we will convert either xml file to csv file or xml string to csv string. When we convert xml string to csv string you will see the output on the browser and when we convert xml file to csv file then you will see the csv document saved into a file on the physical drive location. So here in this example we will see both examples that may help you to choose the example according to your situations.

People working with different types of data may come across the problem that they need to convert between different data formats. Extensible Markup Language (XML) and Comma Separated Values (CSV) are the most widely used formats for data, and conversion between these two formats needs often to be accomplished. Especially to XML, because this format is very well supported by modern applications, and is very well suited for further data manipulation and customization.

There are many tools available online to convert xml to csv but it is very often quite hard to find the appropriate methods that suits one’s particular needs. So here we are going to write our own program to convert xml to csv using php language. Therefore you don’t need to be dependent on any such particular tool to convert xml to csv document.

Related Posts:

Prerequisites

Knowledge of PHP, CSV and XML, PHP 7.3

Converting XML to CSV String

The below PHP code will be used to convert xml to csv string.

Here we will get the xml string as an input and show the csv string output on the browser.

First we write below php function in order to convert xml to csv string.

We load the xml string using the PHP’s built-in function and iterate through each parent’s node to get the row value using get_object_vars() method.

In the below source code get_object_vars() method to get the accessible non-static properties of the given object according to scope.

We first retrieve all node names as headers for csv output. Then we put new line break and put all node values thereafter.

function convertXmlToCsvString($xmlString) {
	$xml = simplexml_load_string($xmlString);
	
	$header = false;
	
	$csv = '';
	
	foreach($xml as $key => $value){
		if(!$header) {
			$csv .= implode(array_keys(get_object_vars($value)), ',');			
			$header = true;
		}
		$csv .= nl2br("\n");
		$csv .= implode(get_object_vars($value), ',');
	}
	
	return $csv;
}

Testing the Program

Sample XML String

Now we will test the above function using php code. We assign xml string with sample data into a variable as shown below:

xml_string = '<?xml version="1.0"?><policies><policy><policyID>119736</policyID><statecode>FL</statecode><county>CLAY COUNTY</county><eq_site_limit>498960</eq_site_limit><hu_site_limit>498960</hu_site_limit><fl_site_limit>498960</fl_site_limit><fr_site_limit>498960</fr_site_limit><tiv_2011>498960</tiv_2011><tiv_2012>792148.9</tiv_2012><eq_site_deductible>0</eq_site_deductible><hu_site_deductible>9979.2</hu_site_deductible><fl_site_deductible>0</fl_site_deductible><fr_site_deductible>0</fr_site_deductible><point_latitude>30.102261</point_latitude><point_longitude>-81.711777</point_longitude><line>Residential</line><construction>Masonry</construction><point_granularity>1</point_granularity></policy><policy><policyID>448094</policyID><statecode>FL</statecode><county>CLAY COUNTY</county><eq_site_limit>1322376.3</eq_site_limit><hu_site_limit>1322376.3</hu_site_limit><fl_site_limit>1322376.3</fl_site_limit><fr_site_limit>1322376.3</fr_site_limit><tiv_2011>1322376.3</tiv_2011><tiv_2012>1438163.57</tiv_2012><eq_site_deductible>0</eq_site_deductible><hu_site_deductible>0</hu_site_deductible><fl_site_deductible>0</fl_site_deductible><fr_site_deductible>0</fr_site_deductible><point_latitude>30.063936</point_latitude><point_longitude>-81.707664</point_longitude><line>Residential</line><construction>Masonry</construction><point_granularity>3</point_granularity></policy><policy><policyID>206893</policyID><statecode>FL</statecode><county>CLAY COUNTY</county><eq_site_limit>190724.4</eq_site_limit><hu_site_limit>190724.4</hu_site_limit><fl_site_limit>190724.4</fl_site_limit><fr_site_limit>190724.4</fr_site_limit><tiv_2011>190724.4</tiv_2011><tiv_2012>192476.78</tiv_2012><eq_site_deductible>0</eq_site_deductible><hu_site_deductible>0</hu_site_deductible><fl_site_deductible>0</fl_site_deductible><fr_site_deductible>0</fr_site_deductible><point_latitude>30.089579</point_latitude><point_longitude>-81.700455</point_longitude><line>Residential</line><construction>Wood</construction><point_granularity>1</point_granularity></policy><policy><policyID>333743</policyID><statecode>FL</statecode><county>CLAY COUNTY</county><eq_site_limit>0</eq_site_limit><hu_site_limit>79520.76</hu_site_limit><fl_site_limit>0</fl_site_limit><fr_site_limit>0</fr_site_limit><tiv_2011>79520.76</tiv_2011><tiv_2012>86854.48</tiv_2012><eq_site_deductible>0</eq_site_deductible><hu_site_deductible>0</hu_site_deductible><fl_site_deductible>0</fl_site_deductible><fr_site_deductible>0</fr_site_deductible><point_latitude>30.063236</point_latitude><point_longitude>-81.707703</point_longitude><line>Residential</line><construction>Wood</construction><point_granularity>3</point_granularity></policy></policies>';

Calling the XML to CSV Converter Function

Next we call the above function and echo the output:

$csv = convertXmlToCsvString($xml_string);
echo $csv;

Output on Browser

Now when you run the php file, you should see csv output on the browser as shown in the below image:

convert xml to csv using php

So we have seen how to convert xml to csv using php. Here we have used xml string.

Converting XML to CSV File

Next we will see how to read xml file and produce csv file as an output.

Related Posts:

So create below php function that will convert xml to csv file.

Here in the below source code we load xml data from the input xml file.

Then we iterate through parent node and write down the data to csv file using fputcsv() function.

function convertXmlToCsvFile($xml_file_input, $csv_file_output) {
	$xml = simplexml_load_file($xml_file_input);
	
	$output_file = fopen($csv_file_output, 'w');
	
	$header = false;
	
	foreach($xml as $key => $value){
		if(!$header) {
			fputcsv($output_file, array_keys(get_object_vars($value)));
			$header = true;
		}
		fputcsv($output_file, get_object_vars($value));
	}
	
	fclose($output_file);
}

Testing the Program

Calling the XML to CSV Converter Function

Now we will test the above function using the below php code:

convertXmlToCsvFile("C:\Users\soumi\Desktop\FL_insurance.xml", "C:\Users\soumi\Desktop\FL_insurance.csv");

In the above code FL_insurance.xml is the input file and FL_insurance.csv is the output file.

Output on Browser

The final output in the csv file should be similar to below

convert xml to csv using php

You may also download the input xml file and output csv file here Fl_insurance.

Hope you have got idea how to convert xml to csv using php.

Related Posts:

Thanks for reading.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *