@@ -53,13 +53,11 @@ public function encode($data, $format)
53
53
*/
54
54
public function decode ($ data , $ format )
55
55
{
56
- $ xml = \DOMDocument::loadXML ($ data );
57
- if (!$ xml ->documentElement ->hasChildNodes ()) {
58
- return "" ;
59
- } elseif ($ xml ->documentElement ->childNodes ->length == 1 && $ xml ->documentElement ->firstChild instanceof \DOMText) {
60
- return trim ((string )$ xml ->documentElement ->firstChild ->wholeText );
56
+ $ xml = simplexml_load_string ($ data );
57
+ if (!$ xml ->count ()) {
58
+ return (string ) $ xml ;
61
59
}
62
- return $ this ->parseXml ($ xml-> documentElement );
60
+ return $ this ->parseXml ($ xml );
63
61
}
64
62
65
63
/**
@@ -151,62 +149,48 @@ final protected function isElementNameValid($name)
151
149
}
152
150
153
151
/**
154
- * Parse the input DOMElement into an array
152
+ * Parse the input SimpleXmlElement into an array
155
153
*
156
- * @param DOMElement $node xml to parse
154
+ * @param SimpleXmlElement $node xml to parse
157
155
* @return array
158
156
*/
159
157
private function parseXml ($ node )
160
158
{
161
159
$ data = array ();
162
- foreach ($ node ->childNodes as $ subnode ) {
163
- //When xml is "beautiful" (with tabs and newlines...), tabs and newline are considered as text but we do not want them
164
- if ($ subnode instanceof DOMText && trim ($ subnode ->wholeText ) === "" ) {
165
- continue ;
166
- }
167
- if (!$ subnode ->hasChildNodes ()) {
168
- $ value = "" ;
169
- } elseif ($ subnode ->childNodes ->length == 1 && $ subnode ->firstChild instanceof \DOMText) {
170
- $ value = trim ((string )$ subnode ->firstChild ->wholeText );
171
- } else {
160
+ foreach ($ node ->children () as $ key => $ subnode ) {
161
+ if ($ subnode ->count ()) {
172
162
$ value = $ this ->parseXml ($ subnode );
173
- }
174
-
175
- if ($ subnode ->hasAttributes ()) {
176
- if (is_string ($ value ) && $ value !== "" ) {
177
- $ value = array ('# ' => $ value );
178
- } elseif (is_string ($ value )) {
179
- $ value = array ();
163
+ if ($ subnode ->attributes ()) {
164
+ foreach ($ subnode ->attributes () as $ attrkey => $ attr ) {
165
+ $ value ['@ ' .$ attrkey ] = (string ) $ attr ;
166
+ }
180
167
}
181
- foreach ($ subnode ->attributes as $ attrKey => $ attr ) {
182
- $ value ['@ ' .$ attrKey ] = (string ) $ attr ->value ;
168
+ } elseif ($ subnode ->attributes ()) {
169
+ $ value = array ();
170
+ foreach ($ subnode ->attributes () as $ attrkey => $ attr ) {
171
+ $ value ['@ ' .$ attrkey ] = (string ) $ attr ;
183
172
}
173
+ $ value ['# ' ] = (string ) $ subnode ;
174
+ } else {
175
+ $ value = (string ) $ subnode ;
184
176
}
185
177
186
- if ($ subnode -> tagName === 'item ' ) {
178
+ if ($ key === 'item ' ) {
187
179
if (isset ($ value ['@key ' ])) {
188
- $ key = $ value ['@key ' ];
189
- $ tmp = $ value ['# ' ];
190
- unset($ value ['@key ' ]);
191
- unset($ value ['# ' ]);
192
- if (!empty ($ value )) {
193
- $ data [$ key ] = array_merge (array ('# ' => $ tmp ), $ value );
194
- } else {
195
- $ data [$ key ] = $ tmp ;
196
- }
180
+ $ data [(string )$ value ['@key ' ]] = $ value ['# ' ];
197
181
} elseif (isset ($ data ['item ' ])) {
198
182
$ tmp = $ data ['item ' ];
199
183
unset($ data ['item ' ]);
200
184
$ data [] = $ tmp ;
201
185
$ data [] = $ value ;
202
186
}
203
- } elseif (key_exists ($ subnode -> tagName , $ data )) {
204
- if ((false === is_array ($ data [$ subnode -> tagName ])) || (false === isset ($ data [$ subnode -> tagName ][0 ]))) {
205
- $ data [$ subnode -> tagName ] = array ($ data [$ subnode -> tagName ]);
187
+ } elseif (key_exists ($ key , $ data )) {
188
+ if ((false === is_array ($ data [$ key ])) || (false === isset ($ data [$ key ][0 ]))) {
189
+ $ data [$ key ] = array ($ data [$ key ]);
206
190
}
207
- $ data [$ subnode -> tagName ][] = $ value ;
191
+ $ data [$ key ][] = $ value ;
208
192
} else {
209
- $ data [$ subnode -> tagName ] = $ value ;
193
+ $ data [$ key ] = $ value ;
210
194
}
211
195
}
212
196
return $ data ;
0 commit comments