diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php index c672c0b598b9e..ef3f30ba05791 100644 --- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php @@ -140,26 +140,22 @@ public function decode(string $data, string $format, array $context = []) // todo: throw an exception if the root node name is not correctly configured (bc) if ($rootNode->hasChildNodes()) { - $xpath = new \DOMXPath($dom); - $data = []; - foreach ($xpath->query('namespace::*', $dom->documentElement) as $nsNode) { - $data['@'.$nsNode->nodeName] = $nsNode->nodeValue; + $data = $this->parseXml($rootNode, $context); + if (\is_array($data)) { + $data = $this->addXmlNamespaces($data, $rootNode, $dom); } - unset($data['@xmlns:xml']); - - if (empty($data)) { - return $this->parseXml($rootNode, $context); - } - - return array_merge($data, (array) $this->parseXml($rootNode, $context)); + return $data; } if (!$rootNode->hasAttributes()) { return $rootNode->nodeValue; } - return array_merge($this->parseXmlAttributes($rootNode, $context), ['#' => $rootNode->nodeValue]); + $data = array_merge($this->parseXmlAttributes($rootNode, $context), ['#' => $rootNode->nodeValue]); + $data = $this->addXmlNamespaces($data, $rootNode, $dom); + + return $data; } /** @@ -344,6 +340,19 @@ private function parseXmlValue(\DOMNode $node, array $context = []) return $value; } + private function addXmlNamespaces(array $data, \DOMNode $node, \DOMDocument $document): array + { + $xpath = new \DOMXPath($document); + + foreach ($xpath->query('namespace::*', $node) as $nsNode) { + $data['@'.$nsNode->nodeName] = $nsNode->nodeValue; + } + + unset($data['@xmlns:xml']); + + return $data; + } + /** * Parse the data and convert it to DOMElements. * diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php index 6e1fb514c498a..ce25e95985db6 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php @@ -450,6 +450,17 @@ public function testDecodeWithNamespace() $array = $this->getNamespacedArray(); $this->assertEquals($array, $this->encoder->decode($source, 'xml')); + + $source = ''."\n". + ''. + ''."\n"; + + $this->assertEquals([ + '@xmlns' => 'http://www.w3.org/2005/Atom', + '@xmlns:app' => 'http://www.w3.org/2007/app', + '@app:foo' => 'bar', + '#' => '', + ], $this->encoder->decode($source, 'xml')); } public function testDecodeScalarWithAttribute()