forked from dr5hn/countries-states-cities-database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_xml.php
78 lines (70 loc) · 2.11 KB
/
export_xml.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
<?php
// Require Array2XML class which takes a PHP array and changes it to XML
require_once 'vendor/base.php';
use Spatie\ArrayToXml\ArrayToXml;
$rootDir = dirname(dirname(__FILE__));
$files = array(
'regions' => array(
'from' => '/regions.json',
'to' => '/xml/regions.xml',
'singular' => 'region',
),
'subregions' => array(
'from' => '/subregions.json',
'to' => '/xml/subregions.xml',
'singular' => 'subregion',
),
'countries' => array(
'from' => '/countries.json',
'to' => '/xml/countries.xml',
'singular' => 'country',
),
'states' => array(
'from' => '/states.json',
'to' => '/xml/states.xml',
'singular' => 'state',
),
'cities' => array(
'from' => '/cities.json',
'to' => '/xml/cities.xml',
'singular' => 'city',
),
'states_cities' => array(
'from' => '/states+cities.json',
'to' => '/xml/states+cities.xml',
'singular' => 'state_city',
),
'countries_states' => array(
'from' => '/countries+states.json',
'to' => '/xml/countries+states.xml',
'singular' => 'country_state',
),
'countries_cities' => array(
'from' => '/countries+cities.json',
'to' => '/xml/countries+cities.xml',
'singular' => 'country_city',
),
'countries_states_cities' => array(
'from' => '/countries+states+cities.json',
'to' => '/xml/countries+states+cities.xml',
'singular' => 'country_state_city',
),
);
foreach ($files as $root => $v) :
// Gets JSON file
$json = file_get_contents($rootDir . $v['from']);
$csc = array($v['singular'] => json_decode($json, true));
// Converts PHP Array to XML with the root element being 'root-element-here'
$xml = ArrayToXml::convert(
$csc,
$root,
false,
'UTF-8',
'1.0',
['formatOutput' => true]
);
$fp = fopen($rootDir . $v['to'], 'w'); // Writing XML to File
fwrite($fp, $xml);
fclose($fp);
echo 'XML Exported to ' . $rootDir . $v['to'] . PHP_EOL;
endforeach;