diff --git a/cake/libs/xml.php b/cake/libs/xml.php index c82cd65e7..fa783ffd1 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.php @@ -85,12 +85,18 @@ class Xml { } elseif ($value === null) { $value = ''; } - if ($format === 'tags') { + if ($key[0] !== '@' && $format === 'tags') { $node->addChild($key, $value); } else { + if ($key[0] === '@') { + $key = substr($key, 1); + } $node->addAttribute($key, $value); } } else { + if ($key[0] === '@') { + throw new Exception(__('Invalid array')); + } if (array_keys($value) === range(0, count($value) - 1)) { // List foreach ($value as $item) { $child = $node->addChild($key); diff --git a/cake/tests/cases/libs/xml.test.php b/cake/tests/cases/libs/xml.test.php index 1f2553cb5..1b5d77d0e 100644 --- a/cake/tests/cases/libs/xml.test.php +++ b/cake/tests/cases/libs/xml.test.php @@ -157,6 +157,24 @@ class XmlTest extends CakeTestCase { $this->assertIdentical((string)$obj->string, 'ok'); $this->assertIdentical((string)$obj->null, ''); $this->assertIdentical((string)$obj->array, ''); + + $xml = array( + 'tags' => array( + 'tag' => array( + array( + '@id' => '1', + 'name' => 'defect' + ), + array( + '@id' => '2', + 'name' => 'enhancement' + ) + ) + ) + ); + $obj = Xml::fromArray($xml, 'tags'); + $xmlText = '<' . '?xml version="1.0"?>defectenhancement'; + $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText); } /** @@ -219,6 +237,26 @@ class XmlTest extends CakeTestCase { $this->assertEqual($e->getMessage(), __('Invalid array')); } + try { + $xml = array( + 'tags' => array( + '@tag' => array( + array( + '@id' => '1', + 'name' => 'defect' + ), + array( + '@id' => '2', + 'name' => 'enhancement' + ) + ) + ) + ); + $obj = Xml::fromArray($xml); + $this->fail('No exception thrown'); + } catch (Exception $e) { + $this->assertEqual($e->getMessage(), __('Invalid array')); + } } /** @@ -302,6 +340,7 @@ class XmlTest extends CakeTestCase { ) ); $this->assertEqual(Xml::toArray(Xml::fromArray($array)), $expected); + $this->assertEqual(Xml::toArray(Xml::fromArray($array, 'tags')), $array); $array = array( 'tags' => array(