@@ -131,21 +131,24 @@ public function convertFrom($inputXml)
131
131
$ this ->loadXml ($ inputXml );
132
132
133
133
// Convert the XML to an array, starting with the root node
134
- $ rootNode = $ this ->xml ->documentElement ->nodeName ;
135
- $ this ->array [$ rootNode ] = $ this ->parseNode ($ this ->xml ->documentElement );
134
+ $ rootNode = $ this ->xml ->documentElement ;
135
+ $ rootValue = $ this ->parseNode ($ rootNode );
136
+ $ rootNodeName = $ rootNode ->nodeName ;
137
+
138
+ $ this ->array [$ rootNodeName ] = $ rootValue ;
136
139
137
140
// Add namespacing information to the root node
138
141
if (!empty ($ this ->namespaces ) && $ this ->config ['namespacesOnRoot ' ]) {
139
- if (!isset ($ this ->array [$ rootNode ][$ this ->config ['attributesKey ' ]])) {
140
- $ this ->array [$ rootNode ][$ this ->config ['attributesKey ' ]] = [];
142
+ if (!isset ($ this ->array [$ rootNodeName ][$ this ->config ['attributesKey ' ]])) {
143
+ $ this ->array [$ rootNodeName ][$ this ->config ['attributesKey ' ]] = [];
141
144
}
142
145
143
146
foreach ($ this ->namespaces as $ uri => $ prefix ) {
144
147
if ($ prefix ) {
145
148
$ prefix = self ::ATTRIBUTE_NAMESPACE_SEPARATOR . $ prefix ;
146
149
}
147
150
148
- $ this ->array [$ rootNode ][$ this ->config ['attributesKey ' ]][self ::ATTRIBUTE_NAMESPACE . $ prefix ] = $ uri ;
151
+ $ this ->array [$ rootNodeName ][$ this ->config ['attributesKey ' ]][self ::ATTRIBUTE_NAMESPACE . $ prefix ] = $ uri ;
149
152
}
150
153
}
151
154
@@ -186,6 +189,7 @@ public function toJson($options = 0)
186
189
protected function loadXml ($ inputXml )
187
190
{
188
191
$ this ->xml = new DOMDocument ($ this ->config ['version ' ], $ this ->config ['encoding ' ]);
192
+ // $this->xml->preserveWhiteSpace = false;
189
193
190
194
if (is_string ($ inputXml )) {
191
195
$ this ->xml ->loadXML ($ inputXml );
@@ -208,7 +212,7 @@ protected function loadXml($inputXml)
208
212
protected function parseNode (DOMNode $ node )
209
213
{
210
214
$ output = [];
211
- $ output = $ this ->collectNamespaces ($ node , $ output );
215
+ $ output = $ this ->collectNodeNamespaces ($ node , $ output );
212
216
213
217
switch ($ node ->nodeType ) {
214
218
case XML_CDATA_SECTION_NODE :
@@ -316,18 +320,45 @@ protected function collectAttributes(DOMNode $node, $output)
316
320
}
317
321
318
322
$ attributes = [];
323
+ $ namespaces = [];
319
324
320
325
foreach ($ node ->attributes as $ attributeName => $ attributeNode ) {
321
326
$ attributeName = $ attributeNode ->nodeName ;
322
327
$ attributes [$ attributeName ] = (string ) $ attributeNode ->value ;
328
+
329
+ if ($ attributeNode ->namespaceURI ) {
330
+ $ nsUri = $ attributeNode ->namespaceURI ;
331
+ $ nsPrefix = $ attributeNode ->lookupPrefix ($ nsUri );
332
+
333
+ $ namespaces = $ this ->collectNamespaces ($ attributeNode );
334
+ }
323
335
}
324
336
325
337
// if its a leaf node, store the value in @value instead of directly it.
326
338
if (!is_array ($ output )) {
327
339
$ output = [$ this ->config ['valueKey ' ] => $ output ];
328
340
}
329
341
330
- $ output [$ this ->config ['attributesKey ' ]] = $ attributes ;
342
+ $ output [$ this ->config ['attributesKey ' ]] = array_merge ($ attributes , $ namespaces );
343
+
344
+ return $ output ;
345
+ }
346
+
347
+ /**
348
+ * Collect namespaces for special DOMNode
349
+ *
350
+ * @param DOMNode $node
351
+ * @param array $output
352
+ *
353
+ * @return array
354
+ */
355
+ protected function collectNodeNamespaces (DOMNode $ node , array $ output )
356
+ {
357
+ $ namespaces = $ this ->collectNamespaces ($ node );
358
+
359
+ if (!empty ($ namespaces )) {
360
+ $ output [$ this ->config ['attributesKey ' ]] = $ namespaces ;
361
+ }
331
362
332
363
return $ output ;
333
364
}
@@ -336,12 +367,13 @@ protected function collectAttributes(DOMNode $node, $output)
336
367
* Get the namespace of the supplied node, and add it to the list of known namespaces for this document
337
368
*
338
369
* @param DOMNode $node
339
- * @param mixed $output
340
370
*
341
371
* @return mixed
342
372
*/
343
- protected function collectNamespaces (DOMNode $ node, $ output )
373
+ protected function collectNamespaces (DOMNode $ node )
344
374
{
375
+ $ namespaces = [];
376
+
345
377
if ($ node ->namespaceURI ) {
346
378
$ nsUri = $ node ->namespaceURI ;
347
379
$ nsPrefix = $ node ->lookupPrefix ($ nsUri );
@@ -354,11 +386,11 @@ protected function collectNamespaces(DOMNode $node, $output)
354
386
$ nsPrefix = self ::ATTRIBUTE_NAMESPACE_SEPARATOR . $ nsPrefix ;
355
387
}
356
388
357
- $ output [ $ this -> config [ ' attributesKey ' ]] [self ::ATTRIBUTE_NAMESPACE . $ nsPrefix ] = $ nsUri ;
389
+ $ namespaces [self ::ATTRIBUTE_NAMESPACE . $ nsPrefix ] = $ nsUri ;
358
390
}
359
391
}
360
392
}
361
393
362
- return $ output ;
394
+ return $ namespaces ;
363
395
}
364
396
}
0 commit comments