Convert XML to CSV in PHP -


Can an XML file be converted to CSV using PHP?

"post-text" itemprop = "text">

Use search engines to code samples like

PHP 5.0 It is very easy to create a CSV file from XML, we just have to write a few lines

We will use the simple XML extension coming from PHP 5.0.

Simple XML reads a whole XML in an object that we can repeat through its properties. To write the CSV output file we will use fputcsv

fputcsv forms one line as CSV and writes it in the file.

Suppose we are doing this XML with cars.xml:

   

First, let us read our XML using simplexml_load_file to pass the file name and give an object with all properties and values ​​of CSV:

 < Code> $ Xml = simplexml_load_file ($ filexml);  

After reading this, we should be repeated through all the hair nodes of cars and should be written in the output file so that the object, delimiter and enclosure can be specified. For the first time, we should change the object in the array to write the object in CSV:

($ xml-> car $ car as) fputcsv ($ f, get_object_vars ($ car) ), ',', '' ');

Here is a complete source code that converts XML to CSV in php 5.0:

   car as car $) {fputcsv ($ f, get_object_vars ($ car), ',', '' '); } Fclose ($ f); }? & Gt;  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -