From b862d6801680b1d35d700df2e49fbe62e28324f8 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Wed, 28 Jul 2010 19:46:35 -0300 Subject: [PATCH] Support to namespaces in Xml::toArray. --- cake/libs/xml.php | 18 +++++++++++------- cake/tests/cases/libs/xml.test.php | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/cake/libs/xml.php b/cake/libs/xml.php index 2530c2b93..5aecdc845 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.php @@ -118,7 +118,8 @@ class Xml { throw new Exception(__('The input is not instance of SimpleXMLElement.')); } $result = array(); - self::_toArray($simpleXML, $result); + $namespaces = array_merge(array('' => ''), $simpleXML->getNamespaces(true)); + self::_toArray($simpleXML, $result, array_keys($namespaces)); return $result; } @@ -127,17 +128,20 @@ class Xml { * * @param object $xml SimpleXMLElement object * @param array $parentData Parent array with data + * @param array $namespaces List of namespaces in XML * @return void */ - protected static function _toArray($xml, &$parentData) { + protected static function _toArray($xml, &$parentData, $namespaces) { $data = array(); - foreach ($xml->attributes() as $key => $value) { - $data[$key] = (string)$value; - } + foreach ($namespaces as $namespace) { + foreach ($xml->attributes($namespace, true) as $key => $value) { + $data[$key] = (string)$value; + } - foreach ($xml->children() as $child) { - self::_toArray($child, $data); + foreach ($xml->children($namespace, true) as $child) { + self::_toArray($child, $data, $namespaces); + } } $asString = trim((string)$xml); diff --git a/cake/tests/cases/libs/xml.test.php b/cake/tests/cases/libs/xml.test.php index 23b6b604e..3a460154e 100644 --- a/cake/tests/cases/libs/xml.test.php +++ b/cake/tests/cases/libs/xml.test.php @@ -323,6 +323,20 @@ class XmlTest extends CakeTestCase { ) ); $this->assertEqual(Xml::toArray($obj), $expected); + + $xml = ''; + $xml .= 'defect'; + $xml .= '1'; + $xml .= ''; + $obj = Xml::build($xml); + + $expected = array( + 'root' => array( + 'tag' => 'defect', + 'bug' => 1 + ) + ); + $this->assertEqual(Xml::toArray($obj), $expected); } }