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'));
$xmlObject = Xml::build($xmlArray, array('format' => 'tags'));
$xmlString = $xmlObject->asXML();Attention! The default format option was changed from attributes to tags. Then be careful if you depend of this. In the new version you make a mixed array with tags, attributes and value, just use format as tags (or do not say anything, because it is the default value) and put @ before the attributes and in value use @ as key.See the sample:
$xmlArray = array(
'project' => array(
'@id' => 1,
'name' => 'Name of project, as tag',
'@' => 'Value of project'
)
);
$xmlObject = Xml::build($xmlArray);
$xmlString = $xmlObject->asXML();The content of $xmlString will be:
<?xml version="1.0"?>
<project id="1">Value of project<name>Name of project, as tag</name></project>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'); // $xml is now a instace of SimpleXMLElement
//or
$xml = Xml::build('http://bakery.cakephp.org/articles/rss', array('return' => 'simplexml')); // $xml is now a instace of SimpleXMLElement
// New method using DOMDocument
$xml = Xml::build('http://bakery.cakephp.org/articles/rss', array('return' => 'domdocument')); // $xml is now a instace of DOMDocumentThe Xml class of CakePHP 2.0 don't provide the manipulation of content, this must be made in SimpleXMLElement or DOMDocument. But, how CakePHP is so sweet, below has the steps to do for create a child node.
// CakePHP 1.3
$myXmlOriginal = '<?xml version="1.0"?><root><child>value</child></root>';
$xml = new Xml($myXmlOriginal, array('format' => 'tags'));
$xml->children[0]->createNode('young', 'new value');
// CakePHP 2.0 - Using SimpleXML
$myXmlOriginal = '<?xml version="1.0"?><root><child>value</child></root>';
$xml = Xml::build($myXmlOriginal);
$xml->root->addChild('young', 'new value');
// CakePHP 2.0 - Using DOMDocument
$myXmlOriginal = '<?xml version="1.0"?><root><child>value</child></root>';
$xml = Xml::build($myXmlOriginal, array('return' => 'domdocument'));
$child = $xml->createElement('young', 'new value');
$xml->firstChild->appendChild($child);