@@ -131,21 +131,24 @@ public function convertFrom($inputXml)
131131 $ this ->loadXml ($ inputXml );
132132
133133 // 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 ;
136139
137140 // Add namespacing information to the root node
138141 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 ' ]] = [];
141144 }
142145
143146 foreach ($ this ->namespaces as $ uri => $ prefix ) {
144147 if ($ prefix ) {
145148 $ prefix = self ::ATTRIBUTE_NAMESPACE_SEPARATOR . $ prefix ;
146149 }
147150
148- $ this ->array [$ rootNode ][$ this ->config ['attributesKey ' ]][self ::ATTRIBUTE_NAMESPACE . $ prefix ] = $ uri ;
151+ $ this ->array [$ rootNodeName ][$ this ->config ['attributesKey ' ]][self ::ATTRIBUTE_NAMESPACE . $ prefix ] = $ uri ;
149152 }
150153 }
151154
@@ -186,6 +189,7 @@ public function toJson($options = 0)
186189 protected function loadXml ($ inputXml )
187190 {
188191 $ this ->xml = new DOMDocument ($ this ->config ['version ' ], $ this ->config ['encoding ' ]);
192+ // $this->xml->preserveWhiteSpace = false;
189193
190194 if (is_string ($ inputXml )) {
191195 $ this ->xml ->loadXML ($ inputXml );
@@ -208,7 +212,7 @@ protected function loadXml($inputXml)
208212 protected function parseNode (DOMNode $ node )
209213 {
210214 $ output = [];
211- $ output = $ this ->collectNamespaces ($ node , $ output );
215+ $ output = $ this ->collectNodeNamespaces ($ node , $ output );
212216
213217 switch ($ node ->nodeType ) {
214218 case XML_CDATA_SECTION_NODE :
@@ -316,18 +320,45 @@ protected function collectAttributes(DOMNode $node, $output)
316320 }
317321
318322 $ attributes = [];
323+ $ namespaces = [];
319324
320325 foreach ($ node ->attributes as $ attributeName => $ attributeNode ) {
321326 $ attributeName = $ attributeNode ->nodeName ;
322327 $ 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+ }
323335 }
324336
325337 // if its a leaf node, store the value in @value instead of directly it.
326338 if (!is_array ($ output )) {
327339 $ output = [$ this ->config ['valueKey ' ] => $ output ];
328340 }
329341
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+ }
331362
332363 return $ output ;
333364 }
@@ -336,12 +367,13 @@ protected function collectAttributes(DOMNode $node, $output)
336367 * Get the namespace of the supplied node, and add it to the list of known namespaces for this document
337368 *
338369 * @param DOMNode $node
339- * @param mixed $output
340370 *
341371 * @return mixed
342372 */
343- protected function collectNamespaces (DOMNode $ node, $ output )
373+ protected function collectNamespaces (DOMNode $ node )
344374 {
375+ $ namespaces = [];
376+
345377 if ($ node ->namespaceURI ) {
346378 $ nsUri = $ node ->namespaceURI ;
347379 $ nsPrefix = $ node ->lookupPrefix ($ nsUri );
@@ -354,11 +386,11 @@ protected function collectNamespaces(DOMNode $node, $output)
354386 $ nsPrefix = self ::ATTRIBUTE_NAMESPACE_SEPARATOR . $ nsPrefix ;
355387 }
356388
357- $ output [ $ this -> config [ ' attributesKey ' ]] [self ::ATTRIBUTE_NAMESPACE . $ nsPrefix ] = $ nsUri ;
389+ $ namespaces [self ::ATTRIBUTE_NAMESPACE . $ nsPrefix ] = $ nsUri ;
358390 }
359391 }
360392 }
361393
362- return $ output ;
394+ return $ namespaces ;
363395 }
364396}
0 commit comments