diff --git a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
index e6f5ff9b0e..13da7c0ec7 100644
--- a/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
+++ b/src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
@@ -55,7 +55,15 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface
{
$xml = simplexml_load_string($data);
if (!$xml->count()) {
- return (string) $xml;
+ if (!$xml->attributes()) {
+ return (string) $xml;
+ }
+ $data = array();
+ foreach ($xml->attributes() as $attrkey => $attr) {
+ $data['@'.$attrkey] = (string) $attr;
+ }
+ $data['#'] = (string) $xml;
+ return $data;
}
return $this->parseXml($xml);
}
@@ -157,14 +165,14 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface
private function parseXml($node)
{
$data = array();
+ if ($node->attributes()) {
+ foreach ($node->attributes() as $attrkey => $attr) {
+ $data['@'.$attrkey] = (string) $attr;
+ }
+ }
foreach ($node->children() as $key => $subnode) {
if ($subnode->count()) {
$value = $this->parseXml($subnode);
- if ($subnode->attributes()) {
- foreach ($subnode->attributes() as $attrkey => $attr) {
- $value['@'.$attrkey] = (string) $attr;
- }
- }
} elseif ($subnode->attributes()) {
$value = array();
foreach ($subnode->attributes() as $attrkey => $attr) {
diff --git a/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php b/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php
index 80df994b61..20197b854d 100644
--- a/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php
+++ b/tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php
@@ -111,6 +111,32 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
}
+
+ public function testEncodeScalarRootAttributes()
+ {
+ $array = array(
+ '#' => 'Paul',
+ '@gender' => 'm'
+ );
+
+ $expected = ''."\n".
+ ''."\n";
+
+ $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
+ }
+
+ public function testEncodeRootAttributes()
+ {
+ $array = array(
+ 'firstname' => 'Paul',
+ '@gender' => 'm'
+ );
+
+ $expected = ''."\n".
+ ''."\n";
+
+ $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
+ }
public function testEncodeScalarWithAttribute()
{
@@ -159,6 +185,33 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
}
+
+ public function testDecodeScalarRootAttributes()
+ {
+ $source = ''."\n".
+ 'Peter'."\n";
+
+ $expected = array(
+ '#' => 'Peter',
+ '@gender' => 'M'
+ );
+
+ $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
+ }
+
+ public function testDecodeRootAttributes()
+ {
+ $source = ''."\n".
+ 'PeterMac Calloway'."\n";
+
+ $expected = array(
+ 'firstname' => 'Peter',
+ 'lastname' => 'Mac Calloway',
+ '@gender' => 'M'
+ );
+
+ $this->assertEquals($expected, $this->encoder->decode($source, 'xml'));
+ }
public function testDecodeArray()
{