Read/Write An XML File Using CakePHP 1.3

Here’s a snippet that is sure to be quite useful for PHP programmers and web developers who often work with XML files. It’s a code to read/write an XML file by using CakePHP. Some of you might find a few lines a bit familiar, which is because parts of it are borrowed from the concept of parsing XML.

ScreenHunter 170 Sep. 10 17.41 Read/Write An XML File Using CakePHP 1.3
//Save data to an xml
App::import('Helper', 'Xml');
App::import('Core','File');
$xml = new XmlHelper();
$file = $xml->header();
$file .= $xml->serialize($config, array('whitespace' => true));

$xmlFile = new File(path_to_file);
$xmlFile->write($file, 'w');
$xmlFile->close();

//Read data from an XML
if (file_exists(path_to_file)) {
 App::import('Helper', 'Xml');
 $parsed_xml = new XML(path_to_file);
 $data = $parsed_xml->toArray();
 $this->data['Email'] = $data['StdClass'];
}    

Incoming search terms for the article:

Related Posts

Creating a Psychedelic Art Effect in Your Portraits

Inspirational Photo Retouches By Cristian Girotto

Create Your Own Sticker Design Via Photoshop

Creating The Great Gasby Art Deco Style

2 Comments

  1. Jleagle

    09.10.2012

    1.3 is pretty old now.

  2. dogmatic69

    09.11.2012

    When using Cake 1.3 and SimpleXml is available on your server its recommended to use that instead.

    The Xml classes are know to be quite buggy and there is a bit of a performance gain in using SimpleXml

    Since 2.x Cake wraps SimpleXml and DomDocument as its no longer targeted at php4