用于将.CSV文件转换为.XML的PHP​​脚本

时间:2021-10-22 00:25:41

Just wondering if anyone can point me in the direction of some tips / a script that will help me create an XML from an original CSV File, using PHP.

只是想知道是否有人可以指向一些提示/脚本的方向,这将有助于我使用PHP从原始CSV文件创建XML。

Cheers

干杯

3 个解决方案

#1


15  

This is quite easy to do, just look at fgetcsv to read csv files and then DomDocument to write an xml file. This version uses the headers from the file as the keys of the xml document.

这很容易做到,只需看fgetcsv读取csv文件然后再看DomDocument就可以编写一个xml文件。此版本使用文件中的标头作为xml文档的键。

<?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);
}

$strxml = $doc->saveXML();
$handle = fopen($outputFilename, "w");
fwrite($handle, $strxml);
fclose($handle);

#2


6  

The code given above creates an XML document, but does not store it on any physical device. So replace echo $doc->saveXML(); with

上面给出的代码创建了一个XML文档,但不会将其存储在任何物理设备上。所以替换echo $ doc-> saveXML();同

$strxml = $doc->saveXML();
$handle = fopen($outputFilename, "w");
fwrite($handle, $strxml);
fclose($handle);

#3


5  

There are a number of sites out there that will do it for you.

有很多网站会为你做这件事。

If this is going to be a regular process rather than a one-time thing it may be ideal to just parse the CSV and output the XML yourself:

如果这将是一个常规的过程而不是一次性的事情,那么解析CSV并自己输出XML可能是理想的:

$csv = file("path/to/csv.csv");

foreach($csv as $line)
{
    $data = explode(",", $line);
    echo "<xmltag>".$data[0]."</xmltag>";
    //etc...
}

Look up PHP's file and string functions.

查找PHP的文件和字符串函数。

#1


15  

This is quite easy to do, just look at fgetcsv to read csv files and then DomDocument to write an xml file. This version uses the headers from the file as the keys of the xml document.

这很容易做到,只需看fgetcsv读取csv文件然后再看DomDocument就可以编写一个xml文件。此版本使用文件中的标头作为xml文档的键。

<?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);
}

$strxml = $doc->saveXML();
$handle = fopen($outputFilename, "w");
fwrite($handle, $strxml);
fclose($handle);

#2


6  

The code given above creates an XML document, but does not store it on any physical device. So replace echo $doc->saveXML(); with

上面给出的代码创建了一个XML文档,但不会将其存储在任何物理设备上。所以替换echo $ doc-> saveXML();同

$strxml = $doc->saveXML();
$handle = fopen($outputFilename, "w");
fwrite($handle, $strxml);
fclose($handle);

#3


5  

There are a number of sites out there that will do it for you.

有很多网站会为你做这件事。

If this is going to be a regular process rather than a one-time thing it may be ideal to just parse the CSV and output the XML yourself:

如果这将是一个常规的过程而不是一次性的事情,那么解析CSV并自己输出XML可能是理想的:

$csv = file("path/to/csv.csv");

foreach($csv as $line)
{
    $data = explode(",", $line);
    echo "<xmltag>".$data[0]."</xmltag>";
    //etc...
}

Look up PHP's file and string functions.

查找PHP的文件和字符串函数。