码迷,mamicode.com
首页 > Web开发 > 详细

[PHP] csv to xml

时间:2015-05-28 16:05:29      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set(‘display_errors‘, true);
ini_set(‘auto_detect_line_endings‘, true);

$inputFilename    = ‘input.csv‘;
$outputFilename   = ‘output.xml‘;

// Open csv to read
$inputFile  = fopen($inputFilename, ‘rt‘);

// Get the headers of the file
$headers = fgetcsv($inputFile);

// Create a new dom document with pretty formatting
$doc  = new DomDocument();
$doc->formatOutput   = true;

// Add a root node to the document
$root = $doc->createElement(‘rows‘);
$root = $doc->appendChild($root);

// Loop through each row creating a <row> node with the correct data
while (($row = fgetcsv($inputFile)) !== FALSE)
{
 $container = $doc->createElement(‘row‘);

 foreach ($headers as $i => $header)
 {
  $child = $doc->createElement($header);
  $child = $container->appendChild($child);
     $value = $doc->createTextNode($row[$i]);
     $value = $child->appendChild($value);
 }

 $root->appendChild($container);
}

echo $doc->saveXML();

 

[PHP] csv to xml

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4535917.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!