Read/Write data to an XML file


/ Published in: PHP

A simple code to read/write an XML file by using Cakephp 1.3.


Copy this code and paste it in your HTML
  1. //Save data to an xml
  2. App::import('Helper', 'Xml');
  3. App::import('Core','File');
  4. $xml = new XmlHelper();
  5. $file = $xml->header();
  6. $file .= $xml->serialize($config, array('whitespace' => true));
  7.  
  8. $xmlFile = new File(path_to_file);
  9. $xmlFile->write($file, 'w');
  10. $xmlFile->close();
  11.  
  12. //Read data from an XML
  13. if (file_exists(path_to_file)) {
  14. App::import('Helper', 'Xml');
  15. $parsed_xml = new XML(path_to_file);
  16. $data = $parsed_xml->toArray();
  17. $this->data['Email'] = $data['StdClass'];
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.