1
votes

I have researched for an answer and mainly with the help of answers in this question Convert Tab delimited text file to XML, pieced together the following script to read a CSV file line by line and then convert the results to an XML file.

The CSV file has lines with three or more cells in this manner:

John Doe [email protected] 06/07/2012 01:45

When ran in the Interactive PHP shell, the following script ignores the first line of the file and spits out everything, from two lines at at time, inside the first xml tag:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('auto_detect_line_endings', true);
$xmlWriter = new XMLWriter();
$xmlWriter->openUri('/path/to/destination.xml');
$xmlWriter->setIndent(true);
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->startElement('root');

$tsvFile = new SplFileObject('/path/to/destination.csv');
$tsvFile->setFlags(SplFileObject::READ_CSV);
$tsvFile->setCsvControl("\t");
foreach ($tsvFile as $line => $row) {
if($line > 0 && $line !== ' ') {
    $xmlWriter->startElement('item');
            $xmlWriter->writeElement('name', $row[0]); 
            $xmlWriter->writeElement('email', $row[1]);
            $xmlWriter->writeElement('date', $row[2]);
            $xmlWriter->endElement();
    }
}

$xmlWriter->endElement();
$xmlWriter->endDocument(); ?>

To resolve this, I tried the solution here: tab-delimited string to XML with PHP

The following is the modified script:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('auto_detect_line_endings', true);
$xmlWriter = new XMLWriter();
$xmlWriter->openUri('/path/to/destination.xml');
$xmlWriter->setIndent(true);
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->startElement('root');

$tsvFile = new SplFileObject('/path/to/destination.csv');
$tsvFile->setFlags(SplFileObject::READ_CSV);
$tsvFile->setCsvControl("\t");

$lines = explode("\n", $tsvFile);
$tsvData = array();
foreach ($lines as $line ) {
    if($line > 0 ) {
        $tsvData[] = str_getcsv($line, "\t");
        $tsvData[] = str_getcsv($line, "\t");
        foreach ($tsvData as $row) {
        $xmlWriter->writeElement('name', $row[0]); 
        $xmlWriter->writeElement('email', $row[1]);
            $xmlWriter->writeElement('date', $row[2]);
        $xmlWriter->endElement();
         }
    }
}

$xmlWriter->endElement();
$xmlWriter->endDocument();?>

This script creates the xml file but unfortunately produces no output inside of it.

Would someone be able to help me by pointing out where I am going wrong? I am no expert with this but trying my hardest to learn.

Your help is very much appreciated!

2

2 Answers

1
votes

Everywhere in your script that you have "\t", you are specifying a tab character (as you've modified something for TSV files). If you're trying to convert a CSV file (comma-separated list), as a first step, try replacing all instances of "\t" with ",".

1
votes

You seem to making hard work of this.

XML is (at the end of the day) can be expressed as an text file.

While not just either create a string or file.

Read in the CSV, split the columns. Write out the CSV as XML into that string with the appropriate tags. Then load that file or string into a DOM object.