PHP xml to csv
2012-04-17 21:10
302 查看
From: http://codestips.com/php-xml-to-csv/
To create a csv file from a xml in PHP 5.0 it’s very simple, we will just have to write some lines.
We will use the SimpleXML extension that come from PHP 5.0.
SimpleXML reads an entire xml into an object that we can iterate through his properties.
To write to the csv output file we will use fputcsv.
fputcsv formats a line as csv and writes it to the file.
Suppose we are having this xml named cars.xml:
First we should read our xml using simplexml_load_file passing the name of the file and returns an object with all the properties and values of the csv:
After reading it we should iterate through all the child nodes of cars and write it to the output file using fputcsv specifying the object,delimiter and enclosure. We should first convert the object into an array in order to write it to the csv:
Here is the complete source code that converts xml to csv in php 5.0:
To create a csv file from a xml in PHP 5.0 it’s very simple, we will just have to write some lines.
We will use the SimpleXML extension that come from PHP 5.0.
SimpleXML reads an entire xml into an object that we can iterate through his properties.
To write to the csv output file we will use fputcsv.
fputcsv formats a line as csv and writes it to the file.
Suppose we are having this xml named cars.xml:
<?xml version='1.0'?> <cars> <car> <color>blue</color> <price>2000</price> </car> <car> <color>red</color> <price>10000</price> </car> <car> <color>black</color> <price>5000</price> </car> </cars>
First we should read our xml using simplexml_load_file passing the name of the file and returns an object with all the properties and values of the csv:
$xml = simplexml_load_file($filexml);
After reading it we should iterate through all the child nodes of cars and write it to the output file using fputcsv specifying the object,delimiter and enclosure. We should first convert the object into an array in order to write it to the csv:
foreach ($xml->car as $car) fputcsv($f, get_object_vars($car),',','"');
Here is the complete source code that converts xml to csv in php 5.0:
<? $filexml='cars.xml'; if (file_exists($filexml)) { $xml = simplexml_load_file($filexml); $f = fopen('cars.csv', 'w'); foreach ($xml->car as $car) { fputcsv($f, get_object_vars($car),',','"'); } fclose($f); } ?>
Download php source code for converting xml into a csv
相关文章推荐
- [PHP] csv to xml
- Export Silverlight DataGrid to Excel XML/CSV
- Insight into DOMDocument - how to convert data from XML to array in PHP
- 导出 Silverlight DataGrid to Excel XML/CSV
- 导出 Silverlight DataGrid to Excel XML/CSV
- php csv to array (csv 转数组)
- Spring Batch Example – XML File To CSV File(六)
- [PHP] To display some attribute of the node in the XML document.
- Google Merchant How To Setup A Live XML Data Feed With MySql and PHP
- CSVToXML.jar CSV to XML的方法
- PHP XML To Array,将XML转换为数组
- Android Studio插件之Android Strings.xml To CSV Converter(失效)
- php csv to array (csv 转数组)
- PHP Fatal error: Uncaught Error: Call to undefined function xml_parser_create() 引发的问题
- weiphp 安装( 解决 -1:fail to get xml content 问题)
- 解决php问题“XMLReader::open() [xmlreader.open]: Unable to open source data”
- PHP - XML - Exporting Data to XML
- LINQ to XML 从逗号分隔值 (CSV) 文件生成 XML 文件
- PHP 读写 CSV 、XML 文件: SimpleExcel
- Php的数组转换成XML(php Array to Xml)