Media website professional
ZendFramework, Php & Mysql, Jquery, javascript, Css, Html...
Read and write configuration files the format xml with Zend_Config
1. Read ini file.
I have a configuration file /application/configs/db.ini as follows:
<?xml version="1.0" encoding="UTF-8"?> <configdata> <infomation> <webhost>www.example.com</webhost> <database> <adapter>pdo_mysql</adapter> <params> <host>localhost</host> <username>root</username> <password>root</password> <dbname>zftraining</dbname> </params> </database> </infomation> <data extends="infomation"> <webhost>www.example.com</webhost> <database> <adapter>pdo_mysql</adapter> <params> <host>localhost</host> <username>vanlanh</username> <password>123456</password> <dbname>zftraining</dbname> </params> </database> </data> </configdata>
We use the class to read file Zend_Config_Xml db.xml
public function xmlAction(){ $xml = CONFIG_PATH . '/db.xml'; $section = 'data'; $config = new Zend_Config_Xml($xml, $section); echo '<br>' . $config->webhost; echo '<br>' . $config->database->adapter; echo '<br>' . $config->database->params->username; echo '<br><hr>'; echo '<pre>'; print_r($config->toArray()); echo '</pre>'; }
Output:
www.example.com pdo_mysql vanlanh Array ( [webhost] => www.example.com [database] => Array ( [adapter] => pdo_mysql [params] => Array ( [host] => localhost [username] => vanlanh [password] => 123456 [dbname] => zftraining ) ) )
2.Write xml file
public function writeXmlAction(){ $data = array( 'global'=>array( 'smtp'=>array( 'parameters'=>array( 'host' => "smtp.example.com", 'port' => "587", 'username' => "jeky11", 'password' => "hyd3", 'security' => "tls", ) ) ), 'data'=>array( 'version' => "1.1" ) ); $config = new Zend_Config_Writer_Xml(); $filename = CONFIG_PATH . '/my01.xml'; $objConfig = new Zend_Config($data); $config->write($filename,$objConfig); }
My01.xml output file:
<?xml version="1.0"?> <zend-config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/"> <global> <smtp> <parameters> <host>smtp.example.com</host> <port>587</port> <username>jeky11</username> <password>hyd3</password> <security>tls</security> </parameters> </smtp> </global> <data> <version>1.1</version> </data> </zend-config>
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment