diff --git a/cake/libs/xml.php b/cake/libs/xml.php
index 5884b2815..73a5aa62a 100644
--- a/cake/libs/xml.php
+++ b/cake/libs/xml.php
@@ -81,6 +81,13 @@ class Xml {
/**
* Transform an array into a SimpleXMLElement
*
+ * ### Options
+ *
+ * - `format` If create childs ('tags') or attributes ('attribute').
+ * - `version` Version of XML document. Default is 1.0.
+ * - `encoding` Encoding of XML document. Default is the some of application.
+ * - `return` If return object of SimpleXMLElement ('simplexml') or DOMDocument ('domdocument'). Default is SimpleXMLElement.
+ *
* Using the following data:
*
* {{{
@@ -104,10 +111,10 @@ class Xml {
* `description`
*
* @param array $input Array with data
- * @param string $format If create childs ('tags') or attributes ('attribute').
- * @return object SimpleXMLElement
+ * @param array $options The options to use
+ * @return object SimpleXMLElement or DOMDocument
*/
- public static function fromArray($input, $format = 'tags') {
+ public static function fromArray($input, $options = array()) {
if (!is_array($input) || count($input) !== 1) {
throw new Exception(__('Invalid input.'));
}
@@ -115,9 +122,26 @@ class Xml {
if (is_integer($key)) {
throw new Exception(__('The key of input must be alphanumeric'));
}
- $dom = new DOMDocument('1.0');
- self::_fromArray($dom, $dom, $input, $format);
- return new SimpleXMLElement($dom->saveXML());
+
+ if (is_string($options)) {
+ $options = array('format' => $options);
+ }
+ $defaults = array(
+ 'format' => 'tags',
+ 'version' => '1.0',
+ 'encoding' => Configure::read('App.encoding'),
+ 'return' => 'simplexml'
+ );
+ $options = array_merge($defaults, $options);
+
+ $dom = new DOMDocument($options['version'], $options['encoding']);
+ self::_fromArray($dom, $dom, $input, $options['format']);
+
+ $options['return'] = strtolower($options['return']);
+ if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
+ return new SimpleXMLElement($dom->saveXML());
+ }
+ return $dom;
}
/**
diff --git a/cake/tests/cases/libs/xml.test.php b/cake/tests/cases/libs/xml.test.php
index 61c88e233..b682b943b 100644
--- a/cake/tests/cases/libs/xml.test.php
+++ b/cake/tests/cases/libs/xml.test.php
@@ -27,6 +27,29 @@ App::import('Core', 'Xml');
*/
class XmlTest extends CakeTestCase {
+/**
+ * setup method
+ *
+ * @access public
+ * @return void
+ */
+ function setUp() {
+ parent::setup();
+ $this->_appEncoding = Configure::read('App.encoding');
+ Configure::write('App.encoding', 'UTF-8');
+ }
+
+/**
+ * tearDown method
+ *
+ * @access public
+ * @return void
+ */
+ function tearDown() {
+ parent::tearDown();
+ Configure::write('App.encoding', $this->_appEncoding);
+ }
+
/**
* testBuild method
*
@@ -39,7 +62,7 @@ class XmlTest extends CakeTestCase {
$this->assertEqual((string)$obj->getName(), 'tag');
$this->assertEqual((string)$obj, 'value');
- $xml = 'value';
+ $xml = 'value';
$this->assertEqual($obj, Xml::build($xml));
$xml = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'fixtures' . DS . 'sample.xml';
@@ -119,14 +142,14 @@ class XmlTest extends CakeTestCase {
$this->assertTrue($obj instanceof SimpleXMLElement);
$this->assertEqual($obj->getName(), 'tags');
$this->assertEqual(count($obj), 2);
- $xmlText = '<' . '?xml version="1.0"?>';
+ $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?>';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$obj = Xml::fromArray($xml);
$this->assertTrue($obj instanceof SimpleXMLElement);
$this->assertEqual($obj->getName(), 'tags');
$this->assertEqual(count($obj), 2);
- $xmlText = '<' . '?xml version="1.0"?>1defect2enhancement';
+ $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?>1defect2enhancement';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$xml = array(
@@ -171,7 +194,7 @@ class XmlTest extends CakeTestCase {
)
);
$obj = Xml::fromArray($xml, 'tags');
- $xmlText = '<' . '?xml version="1.0"?>defectenhancement';
+ $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?>defectenhancement';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$xml = array(
@@ -191,7 +214,7 @@ class XmlTest extends CakeTestCase {
)
);
$obj = Xml::fromArray($xml, 'tags');
- $xmlText = '<' . '?xml version="1.0"?>All tagsTag 1defectenhancement';
+ $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?>All tagsTag 1defectenhancement';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
$xml = array(
@@ -203,7 +226,7 @@ class XmlTest extends CakeTestCase {
)
);
$obj = Xml::fromArray($xml, 'attributes');
- $xmlText = '<' . '?xml version="1.0"?>defect';
+ $xmlText = '<' . '?xml version="1.0" encoding="UTF-8"?>defect';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
}
@@ -467,7 +490,7 @@ class XmlTest extends CakeTestCase {
);
$this->assertIdentical(Xml::toArray($xml), $expected);
- $xmlText = '1testing';
+ $xmlText = '1testing';
$xml = Xml::build($xmlText);
$expected = array(
'methodResponse' => array(