|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* XML2Array: A class to convert XML to array in PHP |
|
4
|
|
|
* It returns the array which can be converted back to XML using the Array2XML script |
|
5
|
|
|
* It takes an XML string or a DOMDocument object as an input. |
|
6
|
|
|
* |
|
7
|
|
|
* See Array2XML: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes |
|
8
|
|
|
* |
|
9
|
|
|
* Author : Lalit Patel |
|
10
|
|
|
* Website: http://www.lalit.org/lab/convert-xml-to-array-in-php-xml2array |
|
11
|
|
|
* License: Apache License 2.0 |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* Version: 0.1 (07 Dec 2011) |
|
14
|
|
|
* Version: 0.2 (04 Mar 2012) |
|
15
|
|
|
* Fixed typo 'DomDocument' to 'DOMDocument' |
|
16
|
|
|
* |
|
17
|
|
|
* Usage: |
|
18
|
|
|
* $array = XML2Array::createArray($xml); |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Common\Tool; |
|
22
|
|
|
|
|
23
|
|
|
use DOMDocument; |
|
24
|
|
|
use Exception; |
|
25
|
|
|
|
|
26
|
|
|
class XML2Array |
|
27
|
|
|
{ |
|
28
|
|
|
private static $xml = null; |
|
29
|
|
|
private static $encoding = 'UTF-8'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Initialize the root XML node [optional] |
|
33
|
|
|
* @param $version |
|
34
|
|
|
* @param $encoding |
|
35
|
|
|
* @param $format_output |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function init($version = '1.0', $encoding = 'UTF-8', $format_output = true) |
|
38
|
|
|
{ |
|
39
|
|
|
self::$xml = new DOMDocument($version, $encoding); |
|
40
|
|
|
self::$xml->formatOutput = $format_output; |
|
41
|
|
|
self::$encoding = $encoding; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function &createArray($path, $dontDie = false) |
|
45
|
|
|
{ |
|
46
|
|
|
$xml = self::getXMLRoot(); |
|
47
|
|
|
if (!is_string($path)) { |
|
48
|
|
|
throw new Exception('[XML2Array] Not a string.'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$parsed = $xml->load($path); |
|
52
|
|
|
if (!$parsed) { |
|
53
|
|
|
if (!$dontDie) { |
|
54
|
|
|
throw new Exception('[XML2Array] Error parsing the XML string.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$array[$xml->documentElement->tagName] = self::convert($xml->documentElement); |
|
|
|
|
|
|
61
|
|
|
self::$xml = null; // clear the xml node in the class for 2nd time use. |
|
62
|
|
|
|
|
63
|
|
|
return $array; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function &createArrayFromXML($input_xml) |
|
67
|
|
|
{ |
|
68
|
|
|
$xml = self::getXMLRoot(); |
|
69
|
|
|
if (is_string($input_xml)) { |
|
70
|
|
|
$parsed = $xml->loadXML($input_xml); |
|
71
|
|
|
if (!$parsed) { |
|
72
|
|
|
throw new Exception('[XML2Array] Error parsing the XML string.'); |
|
73
|
|
|
} |
|
74
|
|
|
} else { |
|
75
|
|
|
if (get_class($input_xml) != 'DOMDocument') { |
|
76
|
|
|
throw new Exception('[XML2Array] The input XML object should be of type: DOMDocument.'); |
|
77
|
|
|
} |
|
78
|
|
|
$xml = self::$xml = $input_xml; |
|
79
|
|
|
} |
|
80
|
|
|
$array[$xml->documentElement->tagName] = self::convert($xml->documentElement); |
|
|
|
|
|
|
81
|
|
|
self::$xml = null; // clear the xml node in the class for 2nd time use. |
|
82
|
|
|
|
|
83
|
|
|
return $array; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Convert an Array to XML |
|
88
|
|
|
* @param mixed $node - XML as a string or as an object of DOMDocument |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
|
|
private static function &convert($node) |
|
92
|
|
|
{ |
|
93
|
|
|
$output = []; |
|
94
|
|
|
|
|
95
|
|
|
switch ($node->nodeType) { |
|
96
|
|
|
case XML_CDATA_SECTION_NODE: |
|
97
|
|
|
$output['@cdata'] = trim($node->textContent); |
|
98
|
|
|
break; |
|
99
|
|
|
|
|
100
|
|
|
case XML_TEXT_NODE: |
|
101
|
|
|
$output = trim($node->textContent); |
|
102
|
|
|
break; |
|
103
|
|
|
|
|
104
|
|
|
case XML_ELEMENT_NODE: |
|
105
|
|
|
// for each child node, call the covert function recursively |
|
106
|
|
|
for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) { |
|
107
|
|
|
$child = $node->childNodes->item($i); |
|
108
|
|
|
$v = self::convert($child); |
|
109
|
|
|
if (isset($child->tagName)) { |
|
110
|
|
|
$t = $child->tagName; |
|
111
|
|
|
|
|
112
|
|
|
// assume more nodes of same kind are coming |
|
113
|
|
|
if (!isset($output[$t])) { |
|
114
|
|
|
$output[$t] = []; |
|
115
|
|
|
} |
|
116
|
|
|
$output[$t][] = $v; |
|
117
|
|
|
} else { |
|
118
|
|
|
//check if it is not an empty text node |
|
119
|
|
|
if ($v !== '') { |
|
120
|
|
|
$output = $v; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (is_array($output)) { |
|
|
|
|
|
|
126
|
|
|
// if only one node of its kind, assign it directly instead if array($value); |
|
127
|
|
|
foreach ($output as $t => $v) { |
|
128
|
|
|
if (is_array($v) && count($v) == 1) { |
|
129
|
|
|
$output[$t] = $v[0]; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
if (empty($output)) { |
|
133
|
|
|
//for empty nodes |
|
134
|
|
|
$output = ''; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// loop through the attributes and collect them |
|
139
|
|
|
if ($node->attributes->length) { |
|
140
|
|
|
$a = []; |
|
141
|
|
|
foreach ($node->attributes as $attrName => $attrNode) { |
|
142
|
|
|
$a[$attrName] = (string)$attrNode->value; |
|
143
|
|
|
} |
|
144
|
|
|
// if its an leaf node, store the value in @value instead of directly storing it. |
|
145
|
|
|
if (!is_array($output)) { |
|
146
|
|
|
$output = ['@value' => $output]; |
|
147
|
|
|
} |
|
148
|
|
|
$output['@attributes'] = $a; |
|
149
|
|
|
} |
|
150
|
|
|
break; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $output; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/* |
|
157
|
|
|
* Get the root XML node, if there isn't one, create it. |
|
158
|
|
|
*/ |
|
159
|
|
|
private static function getXMLRoot() |
|
160
|
|
|
{ |
|
161
|
|
|
if (empty(self::$xml)) { |
|
162
|
|
|
self::init(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return self::$xml; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|