Loop van XML naar CVS
Hi,
Ik heb 2 verschillende en afzonderlijk geprogrammeerd.
Script 1: Doorloop map met XML bestanden en geef deze weer
Script 2: Maak CSV bestand d.m.v. array's
Nu wil ik deze combineren zodat de informatie van XML naar CSV word omgezet
Het werkt, hij leest de xml bestanden en zet de waarde netjes in lijnen, maar de bovenste array wil ik ook mee hebben, dat is namelijk de kop van de CSV file.
Hoe kan ik dat oplossen?
Ik heb 2 verschillende en afzonderlijk geprogrammeerd.
Script 1: Doorloop map met XML bestanden en geef deze weer
Code (php)
Script 2: Maak CSV bestand d.m.v. array's
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Nu wil ik deze combineren zodat de informatie van XML naar CSV word omgezet
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$files = glob('articles/*');
$data = array(
array(
'artikelnaam',
'Specs'
)
);
foreach($files as $file) {
$article = simplexml_load_file($file);
$article_name = $article->LV_ARTICLES_DESCRIPTIONANDMEASURE->LV_ARTICLES_LANG_NL;
$article_specification = $article->LV_ARTICLES_SPECIFICATION->LV_ARTICLES_LANG_NL;
$data = array(
array(
"$article_name",
"$article_specification"
)
);
$fp = fopen('php://output', 'wb');
foreach ( $data as $line ) {
fputcsv($fp, $line);
}
}
fclose($fp);
?>
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$files = glob('articles/*');
$data = array(
array(
'artikelnaam',
'Specs'
)
);
foreach($files as $file) {
$article = simplexml_load_file($file);
$article_name = $article->LV_ARTICLES_DESCRIPTIONANDMEASURE->LV_ARTICLES_LANG_NL;
$article_specification = $article->LV_ARTICLES_SPECIFICATION->LV_ARTICLES_LANG_NL;
$data = array(
array(
"$article_name",
"$article_specification"
)
);
$fp = fopen('php://output', 'wb');
foreach ( $data as $line ) {
fputcsv($fp, $line);
}
}
fclose($fp);
?>
Het werkt, hij leest de xml bestanden en zet de waarde netjes in lijnen, maar de bovenste array wil ik ook mee hebben, dat is namelijk de kop van de CSV file.
Hoe kan ik dat oplossen?
Gewijzigd op 11/10/2018 22:03:20 door Yoeri Achterbergen
Dit lijkt er op
https://www.exchangecore.com/blog/php-output-array-csv-headers/
Let op. Achter de tweede array van $data moet nog een komma op het eind.
https://www.exchangecore.com/blog/php-output-array-csv-headers/
Let op. Achter de tweede array van $data moet nog een komma op het eind.
Gewijzigd op 11/10/2018 20:57:45 door Adoptive Solution
De bovenste array die mag maar 1 keer voorkomen in de hele csv file dat moet de bovenste regel worden.
zoals hieronder
------------|----------
|artikelnaam|Prijs |
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
zoals hieronder
------------|----------
|artikelnaam|Prijs |
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
------------|----------
|loop naam |loop prijs|
Waarom niet even oefenen.
Regel 17-20 in het artikel doet wat u wilt.
Met wat geknutsel kunt u het zo inpassen in uw eigen code.
Of een ander voorbeeld zoeken.
De interwebs staat er vol mee, zag ik.
Toevoeging op 11/10/2018 21:47:13:
Aanvullend.
Regel 28 zet u voor regel 12 met daarachter
fputcsv($fp, $data);
Regel 17-20 in het artikel doet wat u wilt.
Met wat geknutsel kunt u het zo inpassen in uw eigen code.
Of een ander voorbeeld zoeken.
De interwebs staat er vol mee, zag ik.
Toevoeging op 11/10/2018 21:47:13:
Aanvullend.
Regel 28 zet u voor regel 12 met daarachter
fputcsv($fp, $data);
Heb het zo gedaan
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$files = glob('articles/*');
$datahard = array(
array(
'artikelnaam',
'Prijs'
),
);
$fp = fopen('php://output', 'wb');
foreach ( $datahard as $line ) {
fputcsv($fp, $line);
}
foreach($files as $file) {
$article = simplexml_load_file($file);
$article_name = $article->LV_ARTICLES_DESCRIPTIONANDMEASURE->LV_ARTICLES_LANG_NL;
$article_price_incl = number_format((float)$article->LV_ARTICLES_PRICE_SHOW, 2, '.', '');
$data = array(
array(
"$article_name",
"$article_price_incl"
)
);
foreach ( $data as $line ) {
fputcsv($fp, $line);
}
}
fclose($fp);
?>
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$files = glob('articles/*');
$datahard = array(
array(
'artikelnaam',
'Prijs'
),
);
$fp = fopen('php://output', 'wb');
foreach ( $datahard as $line ) {
fputcsv($fp, $line);
}
foreach($files as $file) {
$article = simplexml_load_file($file);
$article_name = $article->LV_ARTICLES_DESCRIPTIONANDMEASURE->LV_ARTICLES_LANG_NL;
$article_price_incl = number_format((float)$article->LV_ARTICLES_PRICE_SHOW, 2, '.', '');
$data = array(
array(
"$article_name",
"$article_price_incl"
)
);
foreach ( $data as $line ) {
fputcsv($fp, $line);
}
}
fclose($fp);
?>
Als dat het beoogde resultaat oplevert, dan is het goed.




