-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2csv.php
113 lines (91 loc) · 3.18 KB
/
xml2csv.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
$xmldata='SOURCE_URL.xml';
$csvFile = 'results.csv';
$xPath = '//product';
$delimiter = ','
// One of this array must be empty, or INCLUDE columns, or EXCLUDE
$included_columns = array(); $excluded_columns = array('description');
// Use function - generates CSV
xml2csv($xmldata, $csvFile, $xPath, $included_columns, $excluded_columns, $delimiter);
function xml2csv($xmlFile, $csvFile, $xPath, $included_columns = array(), $excluded_columns = array(), $delimiter = ',') {
$columns = array(); // All possible columns in XML
$all_data = array();
// Load the XML file
if (function_exists('simplexml_load_file')) {
$xml = simplexml_load_file($xmlFile);
if ($xml !== false) {
// pass
}
else {
print "<h3>Could not load xml file</h3>";
return False;
}
} else {
print "<h3>simplexml_load_file function does not exists! Please, install php-xml</h3>";
return False;
}
// Jump to the specified xpath
$path = $xml->xpath($xPath);
// Loop through the specified xpath
foreach($path as $item) {
$item_data = array();
// Loop through attributes
foreach($item[0]->attributes() as $key => $value) {
if (!in_array($key, $columns)) {
array_push($columns, $key);
}
$item_data[$key] = (string)$value;
}
// Loop through the elements in this xpath
foreach($item as $key => $value) {
//print "$key $value<br>";
if (!in_array($key, $columns)) {
array_push($columns, $key);
}
$item_data[$key] = $value;
}
// Add item data for all data
array_push($all_data, $item_data);
}
// Fill all columns for empty values
foreach($all_data as $item_data) {
foreach ($columns as $column) {
if (!in_array($column, $item_data)) {
$item_data[$column] = NULL;
}
}
}
// Delete excluded columns from columns
if (!empty($included_columns)) {
$columns = $included_columns;
}
else if (!empty($excluded_columns)) {
$columns = array_diff($columns, $excluded_columns);
}
// Cleaned and ordered data
$ordered_all_data = array();
foreach($all_data as $item_data) {
$ordered_item = array();
foreach($columns as $column) {
$ordered_item[$column] = $item_data[$column];
}
array_push($ordered_all_data, $ordered_item);
}
// Open CSV file to write
$fp = fopen($csvFile, 'w');
// Write header to CSV
$fields = implode($delimiter, $columns);
fwrite($fp,$fields.PHP_EOL);
// Write data to CSV
foreach ($ordered_all_data as $item_data) {
$item_values = implode(',', array_values($item_data));
//print_r($item_data);
//fputcsv($fp, $item_values);
//fwrite($fp,$item_values.PHP_EOL);
fputcsv($fp, $item_data, $delimiter);
}
fclose($fp);
print '<h3>'. sizeof($ordered_all_data) . " products have been exported to $csvFile</h3>";
return True;
}
?>