-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_parser.php
58 lines (50 loc) · 1.88 KB
/
xml_parser.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
<?php
$filexml = 'tours.xml';
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$i = 1; // Position counter
$values = []; // PHP array
// Writing column headers
$columns = array('Title', 'Code', 'Duration', 'Start', 'End', 'Inclusions', 'MinPrice');
$fs = fopen('tours.csv', 'w');
fputcsv($fs, $columns);
fclose($fs);
// Iterate through each <tour> node
$node = $xml->xpath('//TOUR');
foreach ($node as $n) {
// Iterate through each child of <tour> node
$child = $xml->xpath('//TOUR[' . $i . ']/*');
$prices = [];
$count = 0;
foreach ($child as $value) {
if (xml_attribute($value, 'EUR')) {
$prices['eur'][$count] = xml_attribute($value, 'EUR');
$prices['discount'][$count] = xml_attribute($value, 'DISCOUNT');
}
$value = str_replace(' ', '', $value);
$value = html_entity_decode($value);
$values[] = trim(strip_tags($value));
$count++;
}
$price_arr = [];
foreach ($prices['eur'] as $key => $price) {
$percent = rtrim($prices['discount'][$key], '%');
$percent_value = ($percent / 100) * $price;
$price_arr[] = $price - $percent_value;
}
$values = array_filter($values);
$values[] = number_format(min($price_arr),2);
// Write to CSV files (appending to column headers)
$fs = fopen('tours.csv', 'a');
fputcsv($fs, $values);
fclose($fs);
$values = []; // Clean out array for next <tour> (i.e., row)
$i++; // Move to next <tour> (i.e., node position)
}
}
function xml_attribute($object, $attribute)
{
if (isset($object[$attribute])) {
return (string)$object[$attribute];
}
}