Repository navigation
2.0 Xml
The Xml class was all refactored. As PHP 5 have SimpleXML and DOMDocument, the CakePHP don't need re-implement methods to XML. The new XML class only will transform an array in SimpleXMLElement or DOMDocument object, and vice versa.
"Ok, very good! But... what I will do with my code???" The instructions below show how migrate your code.
//Old method:
$xmlString = '<?xml version="1.0"?><root><child>value</child></value>';
$xmlObject = new Xml($xmlString);
$xmlArray = $xmlObject->toArray();
// New method:
$xmlString = '<?xml version="1.0"?><root><child>value</child></value>';
$xmlArray = Xml::toArray(Xml::build($xmlString));// Old method:
$xmlArray = array('root' => array('child' => 'value'));
$xmlObject = new Xml($xmlArray, array('format' => 'tags'));
$xmlString = $xmlObject->toString();
// New method:
$xmlArray = array('root' => array('child' => 'value'));
$xmlString = Xml::toString(Xml::build($xmlArray, array('format' => 'tags')));Attention! The default format option was changed from attributes to tags. Then be careful if you depend of this.
Attention 2! The structure of array was changed. Now the child must have in a sub-tree and not in the some tree. Moreover, the strings not will be changed by Inflector. See the sample below:
$oldArray = array(
'Projects' => array(
array(
'Project' => array('id' => 1, 'title' => 'Project 1'),
'Industry' => array('id' => 1, 'name' => 'Industry 1')
),
array(
'Project' => array('id' => 2, 'title' => 'Project 2'),
'Industry' => array('id' => 2, 'name' => 'Industry 2')
)
)
);
$newArray = array(
'projects' => array(
'project' => array(
array(
'id' => 1, 'title' => 'Project 1',
'industry' => array('id' => 1, 'name' => Industry 1')
),
array(
'id' => 2, 'title' => 'Project 2',
'industry' => array('id' => 2, 'name' => Industry 2')
)
)
)
);The both will result the below XML:
<?xml version="1.0"?>
<projects>
<project>
<id>1</id>
<title>Project 1</title>
<industry>
<id>1</id>
<name>Industry 1</name>
</industry>
</project>
<project>
<id>2</id>
<title>Project 2</title>
<industry>
<id>2</id>
<name>Industry 2</name>
</industry>
</project>
</projects>As the constructor of Xml class of CakePHP 1.3, the new class of Xml in CakePHP 2.0 can import files, URLs, XML strings, arrays and more, but using the Xml::build() method. This method will generate a SimpleXMLElement or DOMDocument, depending of your options parameter (default is return SimpleXMLElement). Example:
// Old method:
$xml = new Xml('http://bakery.cakephp.org/articles/rss');
// New method using SimpleXML
$xml = Xml::build('http://bakery.cakephp.org/articles/rss');
//or
$xml = Xml::build('http://bakery.cakephp.org/articles/rss', array('return' => 'simplexml'));
// New method using DOMDocument
$xml = Xml::build('http://bakery.cakephp.org/articles/rss', array('return' => 'domdocument'));