A
A
Anatoly Filippov2021-07-24 11:51:47
Visual Basic
Anatoly Filippov, 2021-07-24 11:51:47

How to do Import data from csv to multiple xml?

Hey guys! I undertook to help the management save them from monotonous work, the solution is somewhere nearby in my head, but I can’t put it together yet. The bottom line is this:

There is an upload to a csv file for 1 month (electric meter readings for every 30 minutes), there are Mosenergosbyt requirements to take readings in xml files according to a given sample, for every day.

The question is. Now, if I had figuratively 30 csv files at the input and I just had to substitute the values ​​in xml, I would still understand. But how to get 30 small xml output from one long input table is not clear.

Please tell me which way to think? Maybe it's easier for me to do it in general in php on the server, so that there is no link to excel

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Gnevyshev, 2021-09-03
@iResource

Maybe it's easier for me to do it in general in php on the server, so that there is no link to excel

That's what I would do.
Through the function, the code of which I will give below, is to read CSV into an array, and then, in iterating over the array, generate the necessary XML through, for example, SimpleXML.
An example of a CSV read function - you can read only the columns you need, save memory:
// чтение CSV в удобный массив
if (!function_exists('csv2array')) {
    // $only_fields - массив с названиями нужных столбцов
    function csv2array($file, $charset_in = 'windows-1251', $charset_out = 'utf-8', $only_fields = [])
    {
        $handle = fopen($file, 'r');
        if ($handle === false) {
            echo 'Не удалось открыть файл '.$file;
            return false;
        }
        $data = $fields = [];
        $i = 0;
        if ($handle) {
            while (($row = fgetcsv($handle, 0, ';')) !== false) {
                if (empty($fields)) {
                    foreach ($row as $k => $r) {
                        $row[$k] = iconv($charset_in, $charset_out.'//IGNORE', $r);
                    }
                    $fields = $row;
                    continue;
                }
                foreach ($row as $k => $value) {
                    if (!empty($fields[$k])) {
                        if (!empty($only_fields) && is_array($only_fields) && !in_array($fields[$k], $only_fields)) {
                            continue;
                        }
                        $value = iconv($charset_in, $charset_out.'//IGNORE', $value);
                        $data[$i][$fields[$k]] = $value;
                    }
                }
                $i++;
            }
            if (!feof($handle)) {
                echo 'Error: unexpected fgets() fail'.PHP_EOL;
            }
            fclose($handle);
        }
        return $data;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question