-
Notifications
You must be signed in to change notification settings - Fork 2
/
json2xml.php
77 lines (64 loc) · 2 KB
/
json2xml.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
<?php
$json = stream_get_contents(STDIN);
$data = @json_decode($json, false);
if (!is_array($data) && !is_object($data)) {
echo 'ERROR: Invalid JSON given' . PHP_EOL;
exit(1);
}
class Exporter
{
private $root = 'document';
private $indentation = ' ';
// TODO: private $this->addtypes = false; // type="string|int|float|array|null|bool"
public function export($data)
{
$data = array($this->root => $data);
echo '<?xml version="1.0" encoding="UTF-8">';
$this->recurse($data, 0);
echo PHP_EOL;
}
private function recurse($data, $level)
{
$indent = str_repeat($this->indentation, $level);
foreach ($data as $key => $value) {
echo PHP_EOL . $indent . '<' . $key;
if ($value === null) {
echo ' />';
} else {
echo '>';
if (is_array($value)) {
if ($value) {
$temporary = $this->getArrayName($key);
foreach ($value as $entry) {
$this->recurse(array($temporary => $entry), $level + 1);
}
echo PHP_EOL . $indent;
}
} else if (is_object($value)) {
if ($value) {
$this->recurse($value, $level + 1);
echo PHP_EOL . $indent;
}
} else {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
echo $this->escape($value);
}
echo '</' . $key . '>';
}
}
}
private function escape($value)
{
// TODO:
return $value;
}
private function getArrayName($parentName)
{
// TODO: special namding for tag names within arrays
return $parentName;
}
}
$exporter = new Exporter();
$exporter->export($data);