diff --git a/cake/libs/xml.php b/cake/libs/xml.php index fa783ffd1..dfffdf904 100644 --- a/cake/libs/xml.php +++ b/cake/libs/xml.php @@ -57,7 +57,12 @@ class Xml { throw new Exception(__('The key of input must be alphanumeric')); } if (is_array($input[$key])) { - $simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . ' />'); + if (array_key_exists('@', $input[$key])) { + $simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . '>' . $input[$key]['@'] . ''); + unset($input[$key]['@']); + } else { + $simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . ' />'); + } self::_fromArrayRecursive($simpleXml, $input[$key], $format); } else { $simpleXml = new SimpleXMLElement('<' . '?xml version="1.0"?' . '><' . $key . '>' . $input[$key] . ''); @@ -99,11 +104,21 @@ class Xml { } if (array_keys($value) === range(0, count($value) - 1)) { // List foreach ($value as $item) { - $child = $node->addChild($key); + if (array_key_exists('@', $item)) { + $child = $node->addChild($key, (string)$item['@']); + unset($item['@']); + } else { + $child = $node->addChild($key); + } self::_fromArrayRecursive($child, $item, $format); } } else { // Struct - $child = $node->addChild($key); + if (array_key_exists('@', $value)) { + $child = $node->addChild($key, (string)$value['@']); + unset($value['@']); + } else { + $child = $node->addChild($key); + } self::_fromArrayRecursive($child, $value, $format); } } @@ -154,7 +169,7 @@ class Xml { if (empty($data)) { $data = $asString; } elseif (!empty($asString)) { - $data['value'] = $asString; + $data['@'] = $asString; } $name = $xml->getName(); diff --git a/cake/tests/cases/libs/xml.test.php b/cake/tests/cases/libs/xml.test.php index 1b5d77d0e..5545775a6 100644 --- a/cake/tests/cases/libs/xml.test.php +++ b/cake/tests/cases/libs/xml.test.php @@ -103,6 +103,11 @@ class XmlTest extends CakeTestCase { $this->assertEqual($obj->getName(), 'tag'); $this->assertEqual((string)$obj, ''); + $xml = array('tag' => array('@' => 'value')); + $obj = Xml::fromArray($xml); + $this->assertEqual($obj->getName(), 'tag'); + $this->assertEqual((string)$obj, 'value'); + $xml = array( 'tags' => array( 'tag' => array( @@ -175,6 +180,38 @@ class XmlTest extends CakeTestCase { $obj = Xml::fromArray($xml, 'tags'); $xmlText = '<' . '?xml version="1.0"?>defectenhancement'; $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText); + + $xml = array( + 'tags' => array( + 'tag' => array( + array( + '@id' => '1', + 'name' => 'defect', + '@' => 'Tag 1' + ), + array( + '@id' => '2', + 'name' => 'enhancement' + ), + ), + '@' => 'All tags' + ) + ); + $obj = Xml::fromArray($xml, 'tags'); + $xmlText = '<' . '?xml version="1.0"?>All tagsTag 1defectenhancement'; + $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText); + + $xml = array( + 'tags' => array( + 'tag' => array( + 'id' => 1, + '@' => 'defect' + ) + ) + ); + $obj = Xml::fromArray($xml); + $xmlText = '<' . '?xml version="1.0"?>defect'; + $this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText); } /** @@ -385,7 +422,7 @@ class XmlTest extends CakeTestCase { 'root' => array( 'tag' => array( '@id' => 1, - 'value' => 'defect' + '@' => 'defect' ) ) );